python基于numpy的多维数组排序
2018-03-09 21:39
567 查看
python基于numpy的多维数组排序
1.根据数组中某一行的大小排序a.T[np.lexsort(a[0,None])].T #按第0行的大小排序 a.T[np.lexsort(a[-1,None])].T #按最后一行的大小排序
>>> a=np.array([[2,7,4,2],[35,9,1,5],[22,12,3,2]]) >>> a array([[ 2, 7, 4, 2], [35, 9, 1, 5], [22, 12, 3, 2]]) >>> a.T[np.lexsort(a[0,None])].T #按第0行的大小排序 array([[ 2, 2, 4, 7], [35, 5, 1, 9], [22, 2, 3, 12]]) >>> a.T[np.lexsort(a[1,None])].T #按第1行的大小排序 array([[ 4, 2, 7, 2], [ 1, 5, 9, 35], [ 3, 2, 12, 22]]) >>> a.T[np.lexsort(a[2,None])].T #按第2行的大小排序 array([[ 2, 4, 7, 2], [ 5, 1, 9, 35], [ 2, 3, 12, 22]])
逆序排序
>>> a.T[np.lexsort(-a[0,None])].T #按第0行的大小逆序排序 array([[ 7, 4, 2, 2], [ 9, 1, 35, 5], [12, 3, 22, 2]])
2.根据数组中某一列的大小排序
a[np.lexsort(a.T[0,None])] #按第0列的大小排序 a[np.lexsort(a.T[-1,None])] #按最后一列的大小排序
>>> a array([[ 2, 7, 4, 2], [35, 9, 1, 5], [22, 12, 3, 2]]) >>> a[np.lexsort(a.T[0,None])] #按第0列的大小排序 array([[ 2, 7, 4, 2], [22, 12, 3, 2], [35, 9, 1, 5]]) >>> a[np.lexsort(a.T[1,None])] #按第1列的大小排序 array([[ 2, 7, 4, 2], [35, 9, 1, 5], [22, 12, 3, 2]]) >>> a[np.lexsort(a.T[2,None])] #按第2列的大小排序 array([[35, 9, 1, 5], [22, 12, 3, 2], [ 2, 7, 4, 2]])
逆序排序
>>> a[np.lexsort(-a.T[0,None])] #按第0列的大小逆序排序 array([[35, 9, 1, 5], [22, 12, 3, 2], [ 2, 7, 4, 2]])
相关文章推荐
- 基于Python Numpy的数组array和矩阵matrix详解
- python:numpy数组运算、排序、统计、随机数生成
- python numpy数组的几种排序方式
- Python多维数组之numpy.array
- Python 排列组合以及多维数组排序
- python 多维数组排序 和 两个list生成dict
- python 用numpy生成多维随机数组
- 基于Python中numpy数组的合并实例讲解
- python 多维数组的排序
- python 多维数组的排序
- python 多维数组(array)排序
- Python: NumPy中的多维数组ndarray
- 【python】numpy数组中按照特征值对特征向量进行排序
- Python numpy数组排序问题
- python numpy 一维数组转变为多维数组的实例
- python中numpy的矩阵、多维数组的用法
- python numpy数组的几种排序方式---by香蕉麦乐迪
- 【python学习笔记】17:numpy数组排序
- 【Python】Numpy扩充数组函数之repeat和tile用法
- Python 学习之中的一个:在Mac OS X下基于Sublime Text搭建开发平台包括numpy,scipy