seemingly simple list indexing problem

Raymond Hettinger python at rcn.com
Tue Jul 29 01:05:58 CEST 2008


[Ervan Ensis]
> I have a list like [108, 58, 68].  I want to return
> the sorted indices of these items in the same order
> as the original list.  So I should return [2, 0, 1]


One solution is to think of the list indexes
being sorted according the their corresponding
values in the input array:

>>> s = [ 108, 58, 68 ]
>>> sorted(range(len(s)), key=s.__getitem__)
[1, 2, 0]


Raymond



More information about the Python-list mailing list