python 实现多维数组(array)排序
作者:caoxuemei12 时间:2022-03-26 07:35:48
关于多维数组如何复合排序
如数组:
>>> import numpy as np
>>> data = np.array([[2,2,5],[2,1,3],[1,2,3],[3,1,4]])
>>>> data
array([[2, 2, 5],
[2, 1, 3],
[1, 2, 3],
[3, 1, 4]])
将数组先按照第一列升序,第二列升序,第三列升序的方式排序:
>>> idex=np.lexsort([data[:,2], data[:,1], data[:,0]])
>>> sorted_data = data[idex, :]
>>> sorted_data
array([[1, 2, 3],
[2, 1, 3],
[2, 2, 5],
[3, 1, 4]])
然后将数组按照第一列降序,第二列升序,第三列升序的方式排序:
>>> idex=np.lexsort([data[:,2], data[:,1], -1*data[:,0]])
>>> sorted_data = data[idex, :]
>>> sorted_data
array([[3, 1, 4],
[2, 1, 3],
[2, 2, 5],
[1, 2, 3]])
补充拓展:python:对多维数组的降序排列
在python中,遗憾的一点是没有对多维数组按照指定维度进行降序排列的方法。
但是,有对一维数组的降序排列,这就足够了!
# Author: Right.Q
# 实现多维矩阵的逆序排列
def descend_sort(array):
'''对三维数组倒序排列'''
[height, width, channel] = array.shape
sortArray = np.zeros([height, width, channel])
for h in range(height):
for w in range(width):
sortArray[h, w, :] = sorted(array[h, w, :], reverse=True)
return sortArray
形参是指定的三维数组,如果更多维的话,自动识别维度即可。
来源:https://blog.csdn.net/daoxiaxingcai46/article/details/78391169
标签:python,多维数组,array,排序
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于OpenCV的PHP图像人脸识别技术
2023-11-23 22:02:54
卓越网的配送服务让我很不满意
2009-03-19 13:49:00
![](https://img.aspxhome.com/file/UploadPic/20093/19/dingdan-600x456-50s.jpg)
python 实现百度网盘非会员上传超过500个文件的方法
2021-01-31 19:45:37
![](https://img.aspxhome.com/file/2023/0/82580_0s.jpg)
XML简易教程之四
2008-09-05 17:19:00
python中kmeans聚类实现代码
2023-09-27 19:49:44
python全栈知识点总结
2021-05-20 01:55:24
Python调用百度AI实现图片上文字识别功能实例
2022-07-27 11:02:44
![](https://img.aspxhome.com/file/2023/8/72188_0s.png)
Pytorch实现基于CharRNN的文本分类与生成示例
2023-06-29 03:40:32
![](https://img.aspxhome.com/file/2023/5/62905_0s.jpg)
概述javascript在Google IE中的调试技巧
2023-08-08 11:50:29
![](https://img.aspxhome.com/file/2023/8/55928_0s.jpg)
用Python识别人脸,人种等各种信息
2023-12-22 08:22:17
![](https://img.aspxhome.com/file/2023/9/72889_0s.jpg)
python实现最速下降法
2023-08-10 18:19:51
![](https://img.aspxhome.com/file/2023/9/62259_0s.jpg)
全屏flash的尺寸分析
2009-02-11 13:22:00
![](https://img.aspxhome.com/file/UploadPic/20092/11/01-11s.jpg)
Python使用py2exe打包程序介绍
2023-11-12 21:29:55
X/HTML5 v.s. XHTML2(II)
2008-06-18 13:19:00
网页iframe元素应用浅析
2009-04-11 18:11:00
JavaScript 实现模态对话框 源代码大全
2023-08-22 20:32:24
Python入门教程2. 字符串基本操作【运算、格式化输出、常用函数】 <font color=red>原创</font>
2021-08-21 17:17:21
asp实现页面延迟运行的两个简单方法
2007-10-16 13:49:00
asp的access数据库备份 压缩 恢复及清理数据库函数
2008-10-31 12:36:00
浅谈python中的数字类型与处理工具
2022-02-09 01:07:30
![](https://img.aspxhome.com/file/2023/9/85169_0s.png)