[Tutor] I need to learn more about sorting!
Kent Johnson
kent37 at tds.net
Tue Jun 24 04:15:34 CEST 2008
On Mon, Jun 23, 2008 at 8:09 PM, Dick Moores <rdm at rcblue.com> wrote:
> def sort_tuple_list_by_2nd_elements(alist):
> alist.sort
Doesn't do anything.
> alist_tup_elements_reversed = []
> for x in alist:
> alist_tup_elements_reversed.append((x[1], x[0]))
You should learn how to use list comprehensions:
alist_tup_elements_reversed.append = [ (x[1], x[0]) for x in alist ]
> alist_tup_elements_reversed.sort()
> print alist_tup_elements_reversed
>
> alist_tup_elements_reversed_and_reversed_again = []
> for x in alist_tup_elements_reversed:
> alist_tup_elements_reversed_and_reversed_again.append((x[1], x[0]))
Another list comp and you have pretty much reinvented
decorate-sort-undecorate (second time in a week for that!) - google it
or see my link in previous thread.
Kent
More information about the Tutor
mailing list