seemingly simple list indexing problem
wolfram.hinderer at googlemail.com
wolfram.hinderer at googlemail.com
Wed Jul 30 17:08:01 EDT 2008
On 29 Jul., 01:05, Raymond Hettinger <pyt... at rcn.com> wrote:
> [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]
>
To get the desired output you have to apply it twice:
>>> sorted(range(len(s)), key=sorted(range(len(s)), key=s.__getitem__).__getitem__)
[2, 0, 1]
Wolfram
More information about the Python-list
mailing list