[Tutor] I need to learn more about sorting!

Dick Moores rdm at rcblue.com
Tue Jun 24 07:15:22 CEST 2008


At 07:15 PM 6/23/2008, Kent Johnson wrote:
>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 ]

Now, that's why I don't use them. They don't make sense, I thought. 
But that's a typo, or a paste-o, right? You meant
alist_tup_elements_reversed = [ (x[1], x[0]) for x in alist ], which works.

That's a start. Thanks

> >     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.

I plan to study your essay of sorting at 
<http://personalpages.tds.net/~kent37/kk/00007.html>.

Also, one of the authors of OOPIP 
(<http://www.prenhall.com/goldwasser/>), Michael Goldwasser (who 
reads this list), wrote me directly, suggesting I skip ahead in his 
book to chapter 14, "Sorting Algorithms", and read the first few 
pages. Looks understandable to me, so I will, even though that's a 
skip-ahead of about 300 pages for me.

Dick




More information about the Tutor mailing list