如何在 Python 中获取已排序数组的索引 [英] How to get indices of a sorted array in Python
问题描述
我有一个数字列表:
myList = [1, 2, 3, 100, 5]
现在如果我对这个列表进行排序以获得[1, 2, 3, 5, 100]
.我想要的是来自按排序顺序的原始列表,即 [0, 1, 2, 4, 3]
--- ala MATLAB 的排序函数,它返回两者值和指数.
如果您使用的是 numpy,则可以使用 argsort() 函数:
<预><代码>>>>导入 numpy>>>numpy.argsort(myList)数组([0, 1, 2, 4, 3])http://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html
这将返回对数组或列表进行排序的参数.
I have a numerical list:
myList = [1, 2, 3, 100, 5]
Now if I sort this list to obtain [1, 2, 3, 5, 100]
.
What I want is the indices of the elements from the
original list in the sorted order i.e. [0, 1, 2, 4, 3]
--- ala MATLAB's sort function that returns both
values and indices.
If you are using numpy, you have the argsort() function available:
>>> import numpy
>>> numpy.argsort(myList)
array([0, 1, 2, 4, 3])
http://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html
This returns the arguments that would sort the array or list.
这篇关于如何在 Python 中获取已排序数组的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!