【Python】【numpy-汇总3】所有排序函数的示例代码

1.排序函数

排序函数

说明

np.sort( ndarray)

排序,返回副本

np.unique(ndarray)

返回ndarray中的元素,排除重复元素之后,并进行排序

np.intersect1d( ndarray1, ndarray2)

np.union1d( ndarray1, ndarray2)

np.setdiff1d( ndarray1, ndarray2)

np.setxor1d( ndarray1, ndarray2)

返回二者的交集并排序。

返回二者的并集并排序。

返回二者的差。

返回二者的对称差

2.函数的使用示例

np.sort

也可以参见官方manual:https://docs.scipy.org/doc/numpy/reference/generated/numpy.sort.html

np.sort(a, axis=-1, kind='quicksort', order=None)

# -*- coding: utf-8 -*-
"""
@author: tom
Talk is cheap, show me the code
Aim:numpy的排序函数示例
"""

import numpy as np

#排序,返回副本
#np.sort(ndarray, axis=-1, kind='quicksort', order=None)
a = np.array([[1,9,2],
              [7,5,6],
              [4,8,3]])

#a中单独元素element所在axis都是0,如1,7,4和9,5,8,以及2,6,3都在axis=0上
#a中最内层的array所在axis都是1,如[1,9,2]和[7,5,6]以及[4,8,3]都在axis=1上
#说明:a是2维array,最大axis就是1,最小axis就是0
ret = np.sort(a) #sort along the last axis
'''
[[1 2 9]
 [5 6 7]
 [3 4 8]]
'''
ret = np.sort(a, axis=0) #sort along the first axis
'''
[[1 5 2]
 [4 8 3]
 [7 9 6]]
'''
ret = np.sort(a, axis=None) #sort the flattened array
'''
[1 2 3 4 5 6 7 8 9]
'''

#按照给定的元素中某个属性进行排序
sampleType = [('name', 'S10'), ('height', float), ('age', int)] #自定义元素结构
samples = [('Tom',   1.8, 36),
          ('Kitty', 1.9, 35),
          ('John',  1.9, 39),
          ('Anna',  1.7, 32)]
a = np.array(samples, dtype=sampleType)
ret = np.sort(a, order='height') #按照height排序
'''
[(b'Anna',  1.7, 32) 
 (b'Tom',   1.8, 36) 
 (b'John',  1.9, 39) 
 (b'Kitty', 1.9, 35)]
'''
ret = np.sort(a, order=['height','age']) #按照height排序,height相等时再按照age排序
'''
[(b'Anna',  1.7, 32) 
 (b'Tom',   1.8, 36) 
 (b'Kitty', 1.9, 35)
 (b'John',  1.9, 39)]
'''
print(id(a),id(ret)) #184520576 184520416

np.unique

#返回ndarray中的元素,排除重复元素之后,并进行排序
#np.unique(ar, return_index=False, return_inverse=False, return_counts=False)
ret = np.unique([1, 1, 2, 2, 3, 3])  #[1 2 3]
ret = np.unique([3, 3, 2, 2, 1, 1])  #[1 2 3]
ret = np.unique([[1,1],[2,2],[3,3]]) #[1 2 3]
ret = np.unique([[3,3],[2,2],[1,1]]) #[1 2 3]

a = np.array([1, 1, 2, 2, 3, 3])
u,indices = np.unique(a, return_index=True)#u=a[indices]
print(u)          #[1 2 3]
print(indices)    #[0 2 4]
print(a[indices]) #[1 2 3]

u,indices = np.unique([1, 1, 2, 2, 3, 3], return_inverse=True)#a=u[indices]
print(u)          #[1 2 3]
print(indices)    #[0 0 1 1 2 2]
print(u[indices]) #[1 1 2 2 3 3]=a

np.intersect1d

#返回二者的交集并排序。
#np.intersect1d( ndarray1, ndarray2, assume_unique=False)
a = np.array([1,2,3,4,5])
b = np.array([4,5,6,7,8])
c = np.array([4,5,6,7,8,9,10,11])
d = np.array([[4,5,6,7],[8,9,10,11]])
ret = np.intersect1d(a, b)#[4 5]
ret = np.intersect1d(a, c)#[4 5]
ret = np.intersect1d(a, d)#[4 5]

np.union1d

#返回两个序列的并集并排序。
#np.union1d( ndarray1, ndarray2)
a = [0, 1, 2]
b = [1, 2, 3]
c = [3, 4, 5]
ret = np.union1d(a, b) #[0 1 2 3]
ret = np.union1d(b, c) #[1 2 3 4 5]

#返回多个序列的并集并排序,可以同时借助functools.reduce函数
from functools import reduce
ret = reduce(np.union1d, (a, b, c)) #[0 1 2 3 4 5]

np.setdiff1d

#返回二者的差。
#np.setdiff1d(ndarray1, ndarray2)
#Return the sorted, unique values in ar1 that are not in ar2.
a = np.array([1,2,3,4,5])
b = np.array([4,5,6,7,8])
c = np.array([[4,5,6,7,8],[9,10,11,12,13]])
ret = np.setdiff1d(a,b) #[1 2 3]
ret = np.setdiff1d(a,c) #[1 2 3]

np.setxor1d

#返回二者的对称差(ar1和ar2的自己独有的元素集合的unique & sorted)
#Find the set exclusive-or of two arrays.
#Return the sorted, unique values that are in only one (not both) of the input arrays.
#np.setxor1d( ndarray1, ndarray2)

a = np.array([1,2,3,4,5])
b = np.array([4,5,6,7,8])
ret = np.setxor1d(a,b) #[1 2 3 6 7 8]

(end)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值