seemingly simple list indexing problem

John Krukoff jkrukoff at ltgc.com
Wed Jul 30 17:20:13 EDT 2008


On Wed, 2008-07-30 at 14:08 -0700, wolfram.hinderer at googlemail.com
wrote:
> 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
> --
> http://mail.python.org/mailman/listinfo/python-list

Thanks, I knew I was missing something simpler.
-- 
John Krukoff <jkrukoff at ltgc.com>
Land Title Guarantee Company




More information about the Python-list mailing list