[Tutor] Original indices after Sorting

Ali Torkamani torkamani at gmail.com
Mon Jul 23 22:12:48 CEST 2012


I found the solution, we should use numpy's argsort(...):


How can we get the indices of values in the original list after sorting a
> list?
>
>>
>> for example:
>>
>> (Pdb) A=[ 1, -1, 0, 7, 9, 1.3, 2.9 ]
>> (Pdb) A.sort()
>> (Pdb) A
>> [-1, 0, 1, 1.3, 2.9, 7, 9]
>> (Pdb)
>>
>>
>> Now I want to have the original indices of the sorted list, i.e:
>>
>> [1, 2, 0, 5, 6, 3, 4]
>>
>>
(Pdb)  A=[ 1, -1, 0, 7, 9, 1.3, 2.9 ]
(Pdb) sort_index = numpy.argsort(A)
(Pdb) sort_index
array([1, 2, 0, 5, 6, 3, 4])
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120723/10f17bf1/attachment.html>


More information about the Tutor mailing list