Python|反转一个numpy数组
我们知道 Numpy 是一个通用的数组处理包,它提供了一个高性能的多维数组对象,以及处理这些数组的工具。让我们讨论如何反转 numpy 数组。方法#1:使用快捷方式
# Python code to demonstrate
# how to reverse numpy array
# using shortcut method
import numpy as np
# initialising numpy array
ini_array = np.array([1, 2, 3, 6, 4, 5])
# printing initial ini_array
print("initial array", str(ini_array))
# printing type of ini_array
print("type of ini_array", type(ini_array))
# using shortcut method to reverse
res = ini_array[::-1]
# printing result
print("final array", str(res))
输出:
initial array [1 2 3 6 4 5]
type of ini_array
final array [5 4 6 3 2 1]
方法#2:使用flipud
函数
# Python code to demonstrate
# how to reverse numpy array
# using flipud method
import numpy as np
# initialising numpy array
ini_array = np.array([1, 2, 3, 6, 4, 5])
# printing initial ini_array
print("initial array", str(ini_array))
# printing type of ini_array
print("type of ini_array", type(ini_array))
# using flipud method to reverse
res = np.flipud(ini_array)
# printing result
print("final array", str(res))
输出:
initial array [1 2 3 6 4 5]
type of ini_array
final array [5 4 6 3 2 1]