sorting list of tuples by second (third...) tuple item
Greg Landrum
drgergl at mindspring.com
Thu Feb 14 09:14:31 EST 2002
"Marcus Stojek" <stojek at part-gmbh.de> wrote in message
news:3c6bbe96.12195968 at news.easynews.net...
> Hi,
>
> I am still struggling with tuple-lists.
>
> How can I sort a list of tuples (all tuples have 4 float items)
> by the second (or third or n-th) tuple item.
Here's the basic incantation:
>>> foo = [(1,2,3),(1,3,3),(1,0,3)]
>>> foo.sort(lambda x,y:cmp(x[1],y[1])) # sort on the second element
>>> foo
[(1, 0, 3), (1, 2, 3), (1, 3, 3)]
> And I have to
> do it fast.
well now, that's a different problem. :-)
Seriously, the default python sort is pretty fast, so give that a try.
-greg
More information about the Python-list
mailing list