dfernan
asked on
Sort a list of lists
if i have a list of lists like this one: (the one i have is obviously much larger but this is the idea):
new = [['dog',2],['cat',5],['bye ',1]]
...
how can i sort it according to the numbers on the second column, i.e., new[0][1]=2....?
i.e., new.sort should become: new.sort(sthg) = [['bye',1],['dog',2],['cat ',5]]
How can i access all the numbers in the second column together like it was a vector?
i.e., new[sthg][sthg] = [2,5,1]
How would you sort it alphabetically up/down?
i.e., alphabetically up: new.sort should become: new.sort(sthg) = [['bye',1],['cat',5],['dog ',2]]
i.e., alphabetically down: new.sort should become: new.sort(sthg) = [['dog',2],['cat',5],['bye ',1]]
Another basic question:
if i have a list L, L[:] means what?????
I am a newbie in python...
Thanks for the help,
Daniel
new = [['dog',2],['cat',5],['bye
...
how can i sort it according to the numbers on the second column, i.e., new[0][1]=2....?
i.e., new.sort should become: new.sort(sthg) = [['bye',1],['dog',2],['cat
How can i access all the numbers in the second column together like it was a vector?
i.e., new[sthg][sthg] = [2,5,1]
How would you sort it alphabetically up/down?
i.e., alphabetically up: new.sort should become: new.sort(sthg) = [['bye',1],['cat',5],['dog
i.e., alphabetically down: new.sort should become: new.sort(sthg) = [['dog',2],['cat',5],['bye
Another basic question:
if i have a list L, L[:] means what?????
I am a newbie in python...
Thanks for the help,
Daniel
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You may also be interested in the 'sorted' and 'reversed' builtins. With the caveat that both of these return new objects, 'sorted' returns a new list and 'reversed' returns an iterator.
>>> help(sorted) # no cmp arg in 3.0
Help on built-in function sorted in module builtins:
sorted(...)
sorted(iterable, key=None, reverse=False) --> new sorted list
>>> help(reversed)
...
reversed(sequence) -> reverse iterator over values of the sequence
...
Also, as with the 'sorted' builtin, the 'cmp' argument to list.sort is not available in future versions of python starting with 3.0 I believe. You should use the 'key' keyword argument instead which requires a slightly different approach. IIRC, the 'key' argument was introduced in python2.4, so it's safe to use in current code (>=python2.4, of course). For example,
>>> help(sorted) # no cmp arg in 3.0
Help on built-in function sorted in module builtins:
sorted(...)
sorted(iterable, key=None, reverse=False) --> new sorted list
>>> help(reversed)
...
reversed(sequence) -> reverse iterator over values of the sequence
...
Also, as with the 'sorted' builtin, the 'cmp' argument to list.sort is not available in future versions of python starting with 3.0 I believe. You should use the 'key' keyword argument instead which requires a slightly different approach. IIRC, the 'key' argument was introduced in python2.4, so it's safe to use in current code (>=python2.4, of course). For example,
# sort it according to the numbers on the second column, i.e., new[0 [1]=2....?
new.sort(key=lambda x: x[1])
# ... or ...
from operator import itemgetter
new.sort(key=itemgetter(1))
# itemgetter has the added advantage of
# making it easier to sort on the 2nd element,
# with a subsort on the first, for example
>>> l = [(1, 2), (3, 0), (1, 1), (2, 0), (1, 0)]
>>> l.sort(key=itemgetter(1, 0))
[(1, 0), (2, 0), (3, 0), (1, 1), (1, 2)]
Open in new window