如何在Python中获取已排序数组的索引 [英] How to get indices of a sorted array in Python
本文介绍了如何在Python中获取已排序数组的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数字列表:
myList = [1, 2, 3, 100, 5]
现在如果我对此列表进行排序以获得 [1,2,3,5,100]
。
我想要的是排序顺序中
原始列表中元素的索引,即 [0,1,2,4,3]
--- ala MATLAB的sort函数,它返回
值和索引。
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.
推荐答案
如果你正在使用numpy,你可以使用argsort()函数:
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屋!
查看全文