python - 对数组中的值进行排序 : 'reverse' is an invalid keyword argument for this function

标签 python arrays python-3.x sorting

我尝试按降序对数组中的值进行排序。如果我按升序尝试它,它会起作用,但是当我按降序执行它时,我会收到错误。

e = np.array([[5.,3.,8.],[6.,7.,1.],[4.,8.,2.]])
e.sort()

结果:

e = array([[3.,5.,8.],[1.,6.,7.],[2.,4.,8.]])

现在按相反顺序:

  e.sort(reverse=True)

结果:

TypeError: 'reverse' is an invalid keyword argument for this function

我还在 from operator import itemgetter 之后尝试了 e.sort(key=itemgetter(1)) 但出现了相同的错误(“reverse”被“key”替换) ')。

为什么会出现这样的情况呢?为什么它不起作用?为什么会出现这个错误(这是使用key或reverse right的方法)?

最佳答案

根据 numpy documentation,您无法使用 keyreverse 关键字参数。您可以按升序对数组进行排序,然后使用 [::-1] 切片或使用 reversed() View 反转它。

关于python - 对数组中的值进行排序 : 'reverse' is an invalid keyword argument for this function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45737546/

相关文章:

python - Matplotlib 在附加轴上的错误位置打勾

javascript - jquery 查找数组中特定的字符串模式

.net - 如何在VB.NET中将字符串转换为字符串数组?

c - 使用双指针打印指针数组

python - 在Python中生成所有可能的排列

python - 滚动不适用于 PySimpleGUI 中的下拉选择

python 解压缩文件并提供 wxPython 状态栏更新

python - Django raw sql/postgres 时区混淆

python - 如何规范化数据帧的列,忽略其中的无值?

python - Flask sqlalchemy 过滤每个对象的关系中的对象