python - 如何在Python 3中按升序对二维数组列进行降序排序
问题描述
这是我的数组:
import numpy as np
boo = np.array([
[10, 55, 12],
[0, 81, 33],
[92, 11, 3]
])
如果我打印:
[[10 55 12]
[ 0 81 33]
[92 11 3]]
如何按升序对数组列进行排序,按降序对行进行排序,如下所示:
[[33 81 92]
[10 12 55]
[0 3 11]]
解决方案
# import the necessary packages.
import numpy as np
# create the array.
boo = np.array([
[10, 55, 12],
[0, 81, 33],
[92, 11, 3]
])
# we use numpy's 'sort' method to conduct the sorting process.
# we first sort the array along the rows.
boo = np.sort(boo, axis=0)
# we print to observe results.
print(boo)
# we therafter sort the resultant array again, this time on the axis of 1/columns.
boo = np.sort(boo, axis=1)
# we thereafter reverse the contents of the array.
print(boo[::-1])
# output shows as follows:
array([[33, 81, 92],
[10, 12, 55],
[ 0, 3, 11]])
推荐阅读
- json.net - 是否可以将 Newtonsoft.Json 配置为忽略具有 [ScriptIgnore] 属性的属性
- instruction-set - 我不明白的 Risc-v 指令
- python-3.x - 你如何以python方式调用子类?
- node.js - 在浏览器中转换 javascript+jsx 源代码的最佳约定?
- spring - Spring REST ResponseEntity当我们有两个对象时如何返回响应
- matlab - 如何在MATLAB中逐字节读取二进制格式
- css - 使用浏览器窗口的 100% 高度/宽度的多列 HTML 网格布局
- android - POST 请求 android 应用程序使用 volley 到具有 API(.net 核心)的本地 Microsft 服务器
- reactjs - 延迟格式化输入
- apache-spark - 在带有 ES Hadoop 的 Spark Structured Streaming 上使用动态索引