Velocity Reviews > Re: seemingly simple list indexing problem

# Re: seemingly simple list indexing problem

Guilherme Polo
Guest
Posts: n/a

 07-28-2008
On Mon, Jul 28, 2008 at 6:24 PM, Ervan Ensis <(E-Mail Removed)> wrote:
> My programming skills are pretty rusty and I'm just learning Python so this
> problem is giving me trouble.
>
> 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]

You could simply do this:

a = [108, 58, 68]
b = sorted(a)
[b.index(c) for c in a]

>
> For a list that's already in order, I'll just return the indices, i.e. [56,
> 66, 76] should return [0, 1, 2]
>
> Any help would be appreciated.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

--
-- Guilherme H. Polo Goncalves

Raymond Hettinger
Guest
Posts: n/a

 07-28-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

Guest
Posts: n/a

 07-30-2008
On 29 Jul., 01:05, Raymond Hettinger <(E-Mail Removed)> 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

John Krukoff
Guest
Posts: n/a

 07-30-2008

On Wed, 2008-07-30 at 14:08 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed)
wrote:
> On 29 Jul., 01:05, Raymond Hettinger <(E-Mail Removed)> 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 <(E-Mail Removed)>
Land Title Guarantee Company

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Derek Cannon Ruby 11 04-20-2010 02:10 AM John Krukoff Python 6 07-30-2008 08:50 PM ricardodefaria Computer Support 6 08-05-2007 04:14 AM Emin Python 4 01-12-2007 02:40 PM Tom Johnson C++ 4 08-15-2003 10:49 PM