Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: seemingly simple list indexing problem

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
wolfram.hinderer@googlemail.com
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
 
Reply With Quote
 
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Elegant Solution to a Seemingly Simple Problem? Derek Cannon Ruby 11 04-20-2010 02:10 AM
Re: seemingly simple list indexing problem John Krukoff Python 6 07-30-2008 08:50 PM
Indexing services under Windows XP SP2 - Can I disable MS Indexing Service to hasten Google's OR does Google Desktop uses this MS Indexing Service? ricardodefaria Computer Support 6 08-05-2007 04:14 AM
How much slower is dict indexing vs. list indexing? Emin Python 4 01-12-2007 02:40 PM
seemingly simple ofstream problem please help Tom Johnson C++ 4 08-15-2003 10:49 PM



Advertisments