如何在Python中获取排序数组的索引

时间:2021-11-05 18:33:44

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.

现在,如果我对这个列表进行排序,得到[1,2,3,5,100]。我想要的是按排序的顺序从原始列表中的元素的索引,即[0,1,2,4,3]——ala MATLAB的排序函数,返回值和指标。

6 个解决方案

#1


109  

If you are using numpy, you have the argsort() function available:

如果正在使用numpy,则可以使用argsort()函数:

>>> import numpy
>>> numpy.argsort(myList)
array([0, 1, 2, 4, 3])

http://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html

http://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html

This returns the arguments that would sort the array or list.

这将返回排序数组或列表的参数。

#2


95  

Something like next:

类似的:

>>> myList = [1, 2, 3, 100, 5]
>>> [i[0] for i in sorted(enumerate(myList), key=lambda x:x[1])]
[0, 1, 2, 4, 3]

enumerate(myList) gives you a list containing tuples of (index, value):

枚举(myList)给您一个包含(索引、值)元组的列表:

[(0, 1), (1, 2), (2, 3), (3, 100), (4, 5)]

You sort the list by passing it to sorted and specifying a function to extract the sort key (the second element of each tuple; that's what the lambda is for. Finally, the original index of each sorted element is extracted using the [i[0] for i in ...] list comprehension.

通过将列表传递给排序并指定一个函数来提取排序键(每个元组的第二个元素;这就是的作用。最后,使用[i[0] for i in…]提取每个排序元素的原始索引。列表理解。

#3


40  

Warning, pros only:

警告,优点:

myList = [1, 2, 3, 100, 5]    
sorted(range(len(myList)),key=myList.__getitem__)

[0, 1, 2, 4, 3]

如何在Python中获取排序数组的索引

#4


14  

The answers with enumerate are nice, but I personally don't like the lambda used to sort by the value. The following just reverses the index and the value, and sorts that. So it'll first sort by value, then by index.

使用enumerate的答案是很好的,但是我个人并不喜欢用lambda来按值进行排序。下面将对索引和值进行反向排序。它首先按值排序,然后按索引排序。

sorted((e,i) for i,e in enumerate(myList))

#5


9  

Updated answer with enumerate and itemgetter:

使用enumerate和itemgetter更新的答案:

sorted(enumerate(a), key=lambda x: x[1])
# [(0, 1), (1, 2), (2, 3), (4, 5), (3, 100)]

Zip the lists together: The first element in the tuple will the index, the second is the value (then sort it using the second value of the tuple x[1], x is the tuple)

将列表压缩在一起:tuple中的第一个元素将作为索引,第二个元素是值(然后使用tuple的第二个值x[1]对它进行排序,x是tuple)

Or using itemgetter from the operatormodule`:

或使用操作模块中的itemgetter ':

from operator import itemgetter
sorted(enumerate(a), key=itemgetter(1))

#6


2  

If you do not want to use numpy,

如果你不想用numpy,

sorted(range(len(seq)), key=seq.__getitem__)

is fastest, as demonstrated here.

如这里所示,是最快的。

#1


109  

If you are using numpy, you have the argsort() function available:

如果正在使用numpy,则可以使用argsort()函数:

>>> import numpy
>>> numpy.argsort(myList)
array([0, 1, 2, 4, 3])

http://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html

http://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html

This returns the arguments that would sort the array or list.

这将返回排序数组或列表的参数。

#2


95  

Something like next:

类似的:

>>> myList = [1, 2, 3, 100, 5]
>>> [i[0] for i in sorted(enumerate(myList), key=lambda x:x[1])]
[0, 1, 2, 4, 3]

enumerate(myList) gives you a list containing tuples of (index, value):

枚举(myList)给您一个包含(索引、值)元组的列表:

[(0, 1), (1, 2), (2, 3), (3, 100), (4, 5)]

You sort the list by passing it to sorted and specifying a function to extract the sort key (the second element of each tuple; that's what the lambda is for. Finally, the original index of each sorted element is extracted using the [i[0] for i in ...] list comprehension.

通过将列表传递给排序并指定一个函数来提取排序键(每个元组的第二个元素;这就是的作用。最后,使用[i[0] for i in…]提取每个排序元素的原始索引。列表理解。

#3


40  

Warning, pros only:

警告,优点:

myList = [1, 2, 3, 100, 5]    
sorted(range(len(myList)),key=myList.__getitem__)

[0, 1, 2, 4, 3]

如何在Python中获取排序数组的索引

#4


14  

The answers with enumerate are nice, but I personally don't like the lambda used to sort by the value. The following just reverses the index and the value, and sorts that. So it'll first sort by value, then by index.

使用enumerate的答案是很好的,但是我个人并不喜欢用lambda来按值进行排序。下面将对索引和值进行反向排序。它首先按值排序,然后按索引排序。

sorted((e,i) for i,e in enumerate(myList))

#5


9  

Updated answer with enumerate and itemgetter:

使用enumerate和itemgetter更新的答案:

sorted(enumerate(a), key=lambda x: x[1])
# [(0, 1), (1, 2), (2, 3), (4, 5), (3, 100)]

Zip the lists together: The first element in the tuple will the index, the second is the value (then sort it using the second value of the tuple x[1], x is the tuple)

将列表压缩在一起:tuple中的第一个元素将作为索引,第二个元素是值(然后使用tuple的第二个值x[1]对它进行排序,x是tuple)

Or using itemgetter from the operatormodule`:

或使用操作模块中的itemgetter ':

from operator import itemgetter
sorted(enumerate(a), key=itemgetter(1))

#6


2  

If you do not want to use numpy,

如果你不想用numpy,

sorted(range(len(seq)), key=seq.__getitem__)

is fastest, as demonstrated here.

如这里所示,是最快的。