python argsort()用法

python argsort()用法

先看一段程序:

def arg_sort():
    import numpy as np
    x = np.array([1, 4, 5, 7, 3, 8, 0, -6])
    a = sorted(x)
    b = x.argsort()
    print("x: ", x)
    print("a: ", a)
    print("b: ", b)
    print("b type: ", type(b))
if __name__ == "__main__":
	arg_sort()

结果输出:

x:  [ 1  4  5  7  3  8  0 -6]
a:  [-6, 0, 1, 3, 4, 5, 7, 8]
b:  [7 6 0 4 1 2 3 5]
b type:  <class 'numpy.ndarray'>

从结果上不难看出,argsort()先对原来的数组进行排序,然后输出排好序的元素在原来数组中的位置。输出类型与输入数组类型一致。

python中源码解释:供参考

    def argsort(self, axis=-1, kind=None, order=None): # real signature unknown; restored from __doc__
        """
        a.argsort(axis=-1, kind=None, order=None)
            Returns the indices that would sort this array.
            Refer to `numpy.argsort` for full documentation.
            See Also
            --------
            numpy.argsort : equivalent function
        """
        pass
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值