指定对哪一层进行排序,如果需排序的是多维数组,特别是那种np.sort()貌似不太友好

from numpy\core\fromnumeric.py

@array_function_dispatch(_sort_dispatcher)
def sort(a, axis=-1, kind=None, order=None):"""Return a sorted copy of an array.返回数组的排序副本。Parameters----------a : array_likeArray to be sorted.axis : int or None, optionalAxis along which to sort. If None, the array is flattened before sorting. The default is -1, which sorts along the last axis.要排序的轴。 如果为None,则在排序之前将数组展平。 默认值为-1,它沿着最后一个轴排序。kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optionalSorting algorithm. The default is 'quicksort'. Note that both 'stable' and 'mergesort' use timsort or radix sort under the covers and, in general, the actual implementation will vary with data type. The 'mergesort' option is retained for backwards compatibility.排序算法。 默认值为“快速排序”。 请注意,“稳定”和“合并排序”在后台都使用timsort或基数排序,并且通常,实际实现会随数据类型而变化。 保留'mergesort'选项是为了向后兼容。.. versionchanged:: 1.15.0.The 'stable' option was added.order : str or list of str, optionalWhen `a` is an array with fields defined, this argument specifies which fields to compare first, second, etc.  A single field can be specified as a string, and not all fields need be specified, but unspecified fields will still be used, in the order in which they come up in the dtype, to break ties.当“ a”是定义了字段的数组时,此参数指定要比较的字段的第一个,第二个等。单个字段可以指定为字符串,并且不需要指定所有字段,但仍将使用未指定的字段, 按照他们在dtype中出现的顺序,打破关系。Returns-------sorted_array : ndarrayArray of the same type and shape as `a`.类型和形状与“ a”相同的数组。See Also--------ndarray.sort : Method to sort an array in-place.就地排序数组的方法。argsort : Indirect sort.间接排序。lexsort : Indirect stable sort on multiple keys.对多个键进行间接稳定排序。searchsorted : Find elements in a sorted array.查找排序数组中的元素。partition : Partial sort.部分排序。Notes-----The various sorting algorithms are characterized by their average speed, worst case performance, work space size, and whether they are stable. A stable sort keeps items with the same key in the same relative order. The four algorithms implemented in NumPy have the following properties:各种分类算法的特征在于它们的平均速度,最坏情况下的性能,工作空间大小以及它们是否稳定。 稳定的排序使具有相同键的项以相同的相对顺序保持。 NumPy中实现的四种算法具有以下属性:=========== ======= ============= ============ ========kind      speed   worst case    work space   stable=========== ======= ============= ============ ========'quicksort'    1     O(n^2)            0          no'heapsort'     3     O(n*log(n))       0          no'mergesort'    2     O(n*log(n))      ~n/2        yes'timsort'      2     O(n*log(n))      ~n/2        yes=========== ======= ============= ============ ========.. note:: The datatype determines which of 'mergesort' or 'timsort' is actually used, even if 'mergesort' is specified. User selection at a finer scale is not currently available.即使指定了“ mergesort”,数据类型也会确定实际使用的是“ mergesort”还是“ timsort”。 目前尚无法进行更精细的用户选择。All the sort algorithms make temporary copies of the data when sorting along any but the last axis.  Consequently, sorting along the last axis is faster and uses less space than sorting along any other axis.除了沿最后一个轴进行排序外,所有排序算法都会临时复制数据。 因此,与沿其他任何轴进行排序相比,沿最后一个轴进行排序速度更快且占用的空间更少。The sort order for complex numbers is lexicographic. If both the real and imaginary parts are non-nan then the order is determined by the real parts except when they are equal, in which case the order is determined by the imaginary parts.复数的排序顺序为字典顺序。 如果实部和虚部都不是NAN,则顺序由实部确定,除非它们相等,在这种情况下,顺序由虚部确定。Previous to numpy 1.4.0 sorting real and complex arrays containing nan values led to undefined behaviour. In numpy versions >= 1.4.0 nan values are sorted to the end. The extended sort order is:在numpy 1.4.0之前,对包含nan值的实数和复杂数组进行排序会导致未定义的行为。 在numpy版本中,> = 1.4.0会将nan值排序到末尾。 扩展的排序顺序为:* Real: [R, nan]* Complex: [R + Rj, R + nanj, nan + Rj, nan + nanj]where R is a non-nan real value. Complex values with the same nan placements are sorted according to the non-nan part if it exists.其中R是非南实数值。 具有相同nan位置的复杂值(如果存在)将根据非nan部分进行排序。Non-nan values are sorted as before.非Nan值的排序方式与以前相同。.. versionadded:: 1.12.0quicksort has been changed to `introsort <https://en.wikipedia.org/wiki/Introsort>`_.When sorting does not make enough progress it switches to `heapsort <https://en.wikipedia.org/wiki/Heapsort>`_.This implementation makes quicksort O(n*log(n)) in the worst case.'stable' automatically chooses the best stable sorting algorithm for the data type being sorted.It, along with 'mergesort' is currently mapped to `timsort <https://en.wikipedia.org/wiki/Timsort>`_ or `radix sort <https://en.wikipedia.org/wiki/Radix_sort>`_ depending on the data type.API forward compatibility currently limits the ability to select the implementation and it is hardwired for the different data types.quicksort已更改为`introsort <https://en.wikipedia.org/wiki/Introsort>`_。如果排序没有取得足够的进展,它将切换到“堆排序<https://en.wikipedia.org/wiki/Heapsort>`_”。在最坏的情况下,此实现使快速排序O(n * log(n))成为可能。“稳定”会自动为排序的数据类型选择最佳的稳定排序算法。目前,它与“ mergesort”一起被映射到“ timsort <https://en.wikipedia.org/wiki/Timsort>” _或“基数排序<https://en.wikipedia.org/wiki/Radix_sort>” _取决于数据类型。API前向兼容性当前限制了选择实现的能力,并且对不同数据类型进行了硬连线。.. versionadded:: 1.17.0Timsort is added for better performance on already or nearly sorted data. On random data timsort is almost identical to  mergesort. It is now used for stable sort while quicksort is  still the default sort if none is chosen. For timsort details,  refer to `CPython listsort.txt  <https://github.com/python/cpython/blob/3.7/Objects/listsort.txt>`_. 'mergesort' and 'stable' are mapped to radix sort for integer data types. Radix sort is an O(n) sort instead of O(n log n).添加Timsort可以提高对已分类或已分类数据的性能。 在随机数据上,timsort与mergesort几乎相同。 现在,它用于稳定排序,而如果未选择,则快速排序仍是默认排序。 有关音符的详细信息,请参考`CPython listsort.txt <https://github.com/python/cpython/blob/3.7/Objects/listsort.txt>`_。 'mergesort'和'stable'映射为整数数据类型的基数排序。 基数排序是O(n)排序,而不是O(n log n)。.. versionchanged:: 1.17.0NaT now sorts to the end of arrays for consistency with NaN.NaT现在会排序到数组的末尾,以与NaN保持一致。Examples-------->>> a = np.array([[1,4],[3,1]])>>> np.sort(a)                # sort along the last axisarray([[1, 4],[1, 3]])>>> np.sort(a, axis=None)     # sort the flattened arrayarray([1, 1, 3, 4])>>> np.sort(a, axis=0)        # sort along the first axisarray([[1, 1],[3, 4]])(axis=0,就是一排一排纵向比较)Use the `order` keyword to specify a field to use when sorting astructured array:>>> dtype = [('name', 'S10'), ('height', float), ('age', int)]>>> values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38),...           ('Galahad', 1.7, 38)]>>> a = np.array(values, dtype=dtype)       # create a structured array>>> np.sort(a, order='height')                        # doctest: +SKIParray([('Galahad', 1.7, 38), ('Arthur', 1.8, 41),('Lancelot', 1.8999999999999999, 38)],dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')])Sort by age, then height if ages are equal:>>> np.sort(a, order=['age', 'height'])               # doctest: +SKIParray([('Galahad', 1.7, 38), ('Lancelot', 1.8999999999999999, 38),('Arthur', 1.8, 41)],dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')])"""if axis is None:# flatten returns (1, N) for np.matrix, so always use the last axisa = asanyarray(a).flatten()axis = -1else:a = asanyarray(a).copy(order="K")a.sort(axis=axis, kind=kind, order=order)return a

示例

在进行成对数组想根据某个性质的排序时,不要使用这个np.sort()函数,如:

import numpy as np
# key为时间,value为(光照度,平均识别个数)
keyword = {'11:30.0': (50000, 13.96), '12:16.0': (54500, 13.20), '13:15.0': (47500, 12.48),'14:22.0': (55450, 12.44), '14:35.0': (55430, 13.72), '17:03.0': (13990, 11.00),'17:38.0': (9058, 11.60), '17:57.0': (5044, 12.46), '18:20.0': (1300, 13.80),'18:25.0': (900, 13.90), '18:28.0': (700, 13.96), '18:31.0': (570, 13.90),'18:33.0': (500, 13.94), '18:34.0': (450, 13.9), '18:35.0': (440, 13.88),'18:36.0': (360, 13.60), '18:37.0': (300, 13.8), '18:39.0': (250, 13.4),'18:40.0': (200, 13.34),'18:42.0': (150, 13.10), '18:44.0': (100, 11.80), '18:44.2': (90, 11.34),'18.44.4': (80, 11.38), '18:44.8': (70, 9.50), '18:45.0': (60, 9.20),'18:46.0': (50, 11.9), '18:46.3': (40, 10.8), '18:46.6': (30, 9.20),'18:49.0': (20, 9.70), '18:49.6': (15, 6.90), '18:50.3': (13, 4.70),'18:50.9': (12, 3.80), '18:51.5': (11, 2.60), '18:52.2': (10, 1.70),'18:52.9': (9, 1.00), '18:53.6': (8, 0.2), '18:54.3': (7, 0.06),'18:55.0': (6, 0.02)}data = []for key in keyword:data.append(keyword[key])data = np.array(data)
data_sort=np.sort(data,axis=0)for i in range(len(data)):print(data[i],'  ',data_sort[i])

结果:

[5.000e+04 1.396e+01]    [6.   0.02]
[5.45e+04 1.32e+01]    [7.   0.06]
[4.750e+04 1.248e+01]    [8.  0.2]
[5.545e+04 1.244e+01]    [9. 1.]
[5.543e+04 1.372e+01]    [10.   1.7]
[1.399e+04 1.100e+01]    [11.   2.6]
[9058.    11.6]    [12.   3.8]
[5044.     12.46]    [13.   4.7]
[1300.    13.8]    [15.   6.9]
[900.   13.9]    [20.   9.2]
[700.    13.96]    [30.   9.2]
[570.   13.9]    [40.   9.5]
[500.    13.94]    [50.   9.7]
[450.   13.9]    [60.  10.8]
[440.    13.88]    [70. 11.]
[360.   13.6]    [80.   11.34]
[300.   13.8]    [90.   11.38]
[250.   13.4]    [100.   11.6]
[200.    13.34]    [150.   11.8]
[150.   13.1]    [200.   11.9]
[100.   11.8]    [250.    12.44]
[90.   11.34]    [300.    12.46]
[80.   11.38]    [360.    12.48]
[70.   9.5]    [440.   13.1]
[60.   9.2]    [450.   13.2]
[50.  11.9]    [500.    13.34]
[40.  10.8]    [570.   13.4]
[30.   9.2]    [700.   13.6]
[20.   9.7]    [900.    13.72]
[15.   6.9]    [1300.    13.8]
[13.   4.7]    [5044.    13.8]
[12.   3.8]    [9058.     13.88]
[11.   2.6]    [1.399e+04 1.390e+01]
[10.   1.7]    [4.75e+04 1.39e+01]
[9. 1.]    [5.00e+04 1.39e+01]
[8.  0.2]    [5.450e+04 1.394e+01]
[7.   0.06]    [5.543e+04 1.396e+01]
[6.   0.02]    [5.545e+04 1.396e+01]

会发现,本应成对的数组都串了

numpy np.sort()函数(指定对某一轴进行排序,返回数组的排序副本)(成对数组不要用这个排,用哪个啥lexsort()或argsort()都行)相关推荐

  1. Numpy:np.tile()函数

    Numpy:np.tile函数 np.arange()函数 np.tile()函数 np.arange()函数 np.arange()函数返回的是一个有终点和起点的固定步长的排列,其中np.arang ...

  2. python numpy np.finfo()函数 eps

    用法 finfo函数是根据括号中的类型来获得信息,获得符合这个类型的数型 例1: import numpy as np a=np.array([[1],[2],[-1],[0]]) b=np.maxi ...

  3. [转载] python numpy np.finfo()函数 eps

    参考链接: Python中的numpy.log2 用法 finfo函数是根据括号中的类型来获得信息,获得符合这个类型的数型 例1: import numpy as np a=np.array([[1] ...

  4. numpy np.sum()函数(求给定轴上的数组元素的总和)(与ndarray.sum()函数等价)

    from numpy\core\fromnumeric.py def sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue, ini ...

  5. python numpy np.convolve()函数(返回两个一维序列的离散线性卷积)

    文章目录 from numpy.core.numeric() 计算流程 from numpy.core.numeric() def convolve(a, v, mode='full'):" ...

  6. [转载] python numpy np.exp()函数

    参考链接: Python中的numpy.exp def exp(x, *args, **kwargs): # real signature unknown; NOTE: unreliably rest ...

  7. python numpy np.fromstring()函数(从字符串文本中提取数字,返回一维数组)(爬虫提取数字挺好用的)

    from numpy\core\multiarray.py def fromstring(string, dtype=None, count=-1, sep=''): # real signature ...

  8. Numpy || np.array()函数用法指南

    1.Numpy ndarray对象 numpy ndarray对象是一个n维数组对象,ndarray只能存储一系列相同元素. #一维数组 [1,2,3,4] #shape(4,)#二维数组 [[1,2 ...

  9. 为sort函数指定排序规则时注意的问题以及错误的写法

    注意: 写排序规则时,return的应该是一个表达式,这样比较方便一些. 当然,也可以写return true或者false,但是这样写的时候一定要写全,否则假如只写 if(···) return t ...

最新文章

  1. Elasticsearch 查询数据的工作原理是什么?
  2. uWSGI+Nginx安装、配置
  3. XP硬盘安装Ubuntu 10.10双系统图解(转)
  4. 如何捕获window.print点击打印或取消_原来1:1的CAD图纸要这样打印!学了这么久才知道...
  5. spring 集成hibernate 连接多数据库 java BaseDao 实现
  6. 【转】聊聊HTTPS和SSL/TLS协议
  7. 沙发家具网站源码_小户型装修不会选家具?大湾网推荐你了解这些装修风格家具,装修省心空间大!...
  8. bzoj 3895: 取石子(博弈)
  9. Linux读写I2C设备I2C_RDWR用法
  10. 全国医疗机构勒索病毒事件公告:阿里云发布公益行动
  11. Matlab系列教程_基础知识_运算符
  12. configure: error: udev support requested but libudev header not installed
  13. mysql高性能sql引擎剖析_Oracle+高性能SQL引擎剖析:SQL优化与调优机制详解-笔记之执行计划(一)...
  14. 液压系统原理动画_「修机」64个基本液压回路原理动画,全部看懂你就能成专家...
  15. 信息安全技术(黑客攻防) 入门
  16. 无数的讽刺侮辱挖苦打击否定不屑与嘲笑,只有罗永浩才撑得住吧
  17. 炫龙T3-pro 9代cpu无csm兼容选项笔记本GPT硬盘纯uefi安装windows7系统方法
  18. SpringCloud——Eureka服务注册和发现
  19. 51NOD L4-第三章 树 刷题记录-zgw
  20. Java 使用iframe的简单功能页面

热门文章

  1. 教你9招最有效防电脑辐射方法
  2. linux qt 5移植,Qt 5.13支持处理Lottie文件,可以方便地进行移植
  3. java map存放班级和姓名_Java 创建一个HashMap对象,并在其中添加学生的姓名和成绩,键为学生姓名,值为学生成绩,使用增强for循环遍历该HashMap,并输出学生成绩。...
  4. 自由自在休闲食品实现奶茶妹的创业梦
  5. 【学习笔记】7、标准数据类型—字符串
  6. 【备用】关于BOM替代物料与CK11N取数逻辑
  7. 【MM模块】Sub Range 供应商子范围
  8. 【Java】MD5字符串的加密解密
  9. 程序间数据共享与传递(2):EXPORT/IMPORT、SAP/ABAP Memory
  10. SAP实施商看SAP在我国的发展