Python内置array数组有sort()函数可以对数组进行排序,将参数reverse值修改为True为降序排列
x = [2, 4, 6, 8, 3, 1]
x.sort() # [1, 2, 3, 4, 6, 8]
x.sort(reverse=True) # [8, 6, 4, 3, 2, 1]
但是在Numpy数组中并没有reverse此参数
TypeError: 'reverse' is an invalid keyword argument for sort()
于是采用下面的写法
import numpy as np
x = np.array([2, 4, 6, 8, 3, 1])
x.sort() # [1 2 3 4 6 8]
x = abs(np.sort(-x)) # [8 6 4 3 2 1] 先取相反数排序,再加上绝对值得到原数组的降序