python中numpy.argsort(),将数组按照大小返回其index

A = array([ 0,  0,  0,  0,  2,  0,  0,  0,  2,  0,  0,  0,  0,  0,  0,  0,  0,
       13,  0,  0,  3,  0,  0,  0,  0,  0,  0,  2,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  4,  0,  1,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  4,  0,  0,  0,  0,  0, 11,  0,  3,
        0,  0,  2, 11,  0,  0,  0,  1,  0,  0,  0,  1,  2,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 11,  0])
# 按照数值大小降序排列,返回对应的index
A.argsort()[::-1]

返回:

array([17, 65, 98, 71, 43, 59, 20, 67, 80, 70, 4, 27, 8, 45, 75, 79, 30,
31, 29, 28, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 26, 99, 25,
24, 1, 2, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19,
21, 22, 44, 23, 49, 46, 47, 77, 78, 81, 82, 83, 84, 85, 86, 87, 88,
89, 90, 91, 92, 93, 94, 95, 96, 97, 76, 74, 73, 57, 48, 50, 51, 52,
53, 54, 55, 56, 58, 72, 60, 61, 62, 63, 64, 66, 68, 69, 0])

# 按照数值大小升序排列,返回对应的index
A.argsort()

返回:

array([ 0, 69, 68, 66, 64, 63, 62, 61, 60, 72, 58, 56, 55, 54, 53, 52,
51, 50, 48, 57, 73, 74, 76, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87,
86, 85, 84, 83, 82, 81, 78, 77, 47, 46, 49, 23, 44, 22, 21, 19, 18,
16, 15, 14, 13, 12, 11, 10, 9, 7, 6, 5, 3, 2, 1, 24, 25, 99,
26, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 28, 29, 31, 30, 79,
75, 45, 8, 27, 4,70, 80, 67, 20, 59, 43, 71, 98, 65, 17])

其他方法:(此方法看似简单,实际亲测时会出现异常,结果并不一定严格按照排序返回index)

# 按照数值大小升序排列,返回对应的index
np.argsort(A)

# 按照数值大小降序排列,返回对应的index
np.argsort(-A)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值