Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > sorting list of tuples by second (third...) tuple item

Reply
Thread Tools

sorting list of tuples by second (third...) tuple item

 
 
Giovanni Toffoli
Guest
Posts: n/a
 
      02-16-2007
Hi,

I'm not in the mailing list.
By Googling, I stepped into this an old post: (Thu Feb 14 20:40:08 CET 2002)
of Jeff Shannon:
http://mail.python.org/pipermail/pyt...ry/128438.html

<<<
def SortOnItem(mylist, index):
templist = [ (line[index], line) for line in mylist ]
templist.sort()
return [ line[1:] for line in templist ]

What this does is build a separate list containing a tuple of the
element that you want to sort on, and the entire line, sorts that
list (by the first element, of course), and then strips that first
element off ..
>>>


It seems to me that the tuples aren't sorted only by the first element but,
I suppose, other elements are also used if needed to discriminate.
In some cases I got some exceptions when an element of the tuple, other than
the first, didn't admit comparison.
In these cases I had to use an ad hoc comparison function.

Regards, Giovanni Toffoli





 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      02-16-2007
Giovanni Toffoli a écrit :
> Hi,
>
> I'm not in the mailing list.
> By Googling, I stepped into this an old post: (Thu Feb 14 20:40:08 CET
> 2002) of Jeff Shannon:
> http://mail.python.org/pipermail/pyt...ry/128438.html
>
> <<<
> def SortOnItem(mylist, index):
> templist = [ (line[index], line) for line in mylist ]
> templist.sort()
> return [ line[1:] for line in templist ]
>
> What this does is build a separate list containing a tuple of the
> element that you want to sort on, and the entire line, sorts that
> list (by the first element, of course), and then strips that first
> element off ..


It's the "decorate/sort/undecorate" pattern. You may also google for
"schwarzian transform"

>>>>

>
> It seems to me that the tuples aren't sorted only by the first element
> but, I suppose, other elements are also used if needed to discriminate.


Yes. When compared, tuples first compare on the first element, then on
the second etc...

> In some cases I got some exceptions when an element of the tuple, other
> than the first, didn't admit comparison.
> In these cases I had to use an ad hoc comparison function.


Did you try the sorted() function ? Used with operator.itemgetter as the
'key' argument, it may do the trick.
 
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
Dictionaries with tuples or tuples of tuples Jon Reyes Python 18 02-19-2013 03:56 AM
Transform two tuples item by item Gnarlodious Python 3 12-20-2011 03:40 AM
List to Tuple and Tuple to List? Davy Python 3 11-07-2007 06:19 PM
Flatten a list/tuple and Call a function with tuples beginner Python 27 07-26-2007 06:51 AM
Why tuple with one item is no tuple Gregor Horvath Python 37 03-30-2005 06:58 AM



Advertisments