471,338 Members | 1,044 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

sorting a list of list

hi,

are there available library or pythonic algorithm for sorting a list
of list depending on the index of the list inside the list of my
choice?

d_list = [
['a', 1, 9],
['b', 2, 8],
['c', 3, 7],
['d', 4, 6],
['e', 5, 5],
]

Thanks
james
Nov 21 '07 #1
2 1391
are there available library or pythonic algorithm for sorting a list
of list depending on the index of the list inside the list of my
choice?
The built-in sorted() function and the sort() method on various
collections take an optional "key=function" keyword paramater
with which you can pass a function (lambdas are convenient) to
extract the bit on which you want to compare:
>>d_list = [
.... ['a', 1, 9],
.... ['b', 2, 8],
.... ['c', 3, 7],
.... ['d', 4, 6],
.... ['e', 5, 5],
.... ]
>>print sorted.__doc__
sorted(iterable, cmp=None, key=None, reverse=False) --new
sorted list
>>sorted(d_list, key=lambda x: x[2]) # sort by the 3rd item
[['e', 5, 5], ['d', 4, 6], ['c', 3, 7], ['b', 2, 8], ['a', 1, 9]]
>>sorted(d_list, key=lambda x: x[1]) # sort by the 2nd item
[['a', 1, 9], ['b', 2, 8], ['c', 3, 7], ['d', 4, 6], ['e', 5, 5]]
>>sorted(d_list, key=lambda x: x[0]) # sort by the 1st item
[['a', 1, 9], ['b', 2, 8], ['c', 3, 7], ['d', 4, 6], ['e', 5, 5]]

-tkc

Nov 21 '07 #2
Tim Chase wrote:
>are there available library or pythonic algorithm for sorting a list
of list depending on the index of the list inside the list of my
choice?

The built-in sorted() function and the sort() method on various
collections take an optional "key=function" keyword paramater
with which you can pass a function (lambdas are convenient) to
extract the bit on which you want to compare:
>>>d_list = [
... ['a', 1, 9],
... ['b', 2, 8],
... ['c', 3, 7],
... ['d', 4, 6],
... ['e', 5, 5],
... ]
>>>print sorted.__doc__
sorted(iterable, cmp=None, key=None, reverse=False) --new
sorted list
>>>sorted(d_list, key=lambda x: x[2]) # sort by the 3rd item
[['e', 5, 5], ['d', 4, 6], ['c', 3, 7], ['b', 2, 8], ['a', 1, 9]]
>>>sorted(d_list, key=lambda x: x[1]) # sort by the 2nd item
[['a', 1, 9], ['b', 2, 8], ['c', 3, 7], ['d', 4, 6], ['e', 5, 5]]
>>>sorted(d_list, key=lambda x: x[0]) # sort by the 1st item
[['a', 1, 9], ['b', 2, 8], ['c', 3, 7], ['d', 4, 6], ['e', 5, 5]]
Not to be too complicated, but there are functions that return a
callable that is faster than a lambda.
>>import operator
Then, instead of "lambda x: x[2]" use "operator.itemgetter(2)" and
instead of "lambda x: x.foo" use "operator.attrgetter('foo')".
--
Nov 21 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Xah Lee | last post: by
19 posts views Thread by Owen T. Soroke | last post: by
20 posts views Thread by martin-g | last post: by
5 posts views Thread by lemlimlee | last post: by
5 posts views Thread by jrod11 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.