我有一个数字列表:
myList = [1, 2, 3, 100, 5]
现在如果我对这个列表进行排序以获得 [1, 2, 3, 5, 100]
。
我想要的是元素的索引
按排序顺序排列的原始列表,即 [0, 1, 2, 4, 3]
--- ala MATLAB 的 sort 函数同时返回
值(value)和指数。
最佳答案
如果您使用的是 numpy,则可以使用 argsort() 函数:
>>> import numpy
>>> numpy.argsort(myList)
array([0, 1, 2, 4, 3])
http://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html
这将返回对数组或列表进行排序的参数。
https://stackoverflow.com/questions/6422700/