先看一段程序:
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