[Tutor] set key to sort
Kent Johnson
kent37 at tds.net
Sat Jan 31 14:14:26 CET 2009
On Sat, Jan 31, 2009 at 4:51 AM, prasad rao <prasadaraon50 at gmail.com> wrote:
> hell
> I got a problem sorting a list of lists.
> ml=
> [[112, 'p'], [114, 'r'], [97, 'a'], [115, 's'], [97, 'a'], [100, 'd'], [97,
> 'a'], [114, 'r'], [97, 'a'], [111, 'o']]
> sorted(ml,key=?)
> How can I formulate a key to sort based on the first element of each list.
Sorting is by default a lexicographic sort, it will sort on the first
element using the second to break ties in the first, etc.
If you really want to sort *only* on the first item (perhaps the
second items are not comparable) then use key=operator.itemgetter(0).
More explanation here:
http://personalpages.tds.net/~kent37/kk/00007.html
http://wiki.python.org/moin/HowTo/Sorting
> sorting list of lists might have been discussed in the resent past.
> Is there a repository of all threads of this list on the web.
Click the link at the bottom of each email to get to the tutor list
home page. From there you can click to the archives.
Kent
> thank you
> prasad
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list