Fun with reverse sorts

David Di Biase dave.dibiase at gmail.com
Fri Oct 3 05:07:02 CEST 2008


Hi there,

I'm sorting an expansive list descending according to a list of tuples.
Basically it has to sort the last value in the tuple (3) but if they are the
same then it should resort to using the second last value (2). Now according
to my very limited testing I've somewhat figured out that this SHOULD work:

list.sort(lambda a, b: (cmp(a[3], b[3]), cmp(a[2], b[2])) [a[3] == b[3]],
reverse = True)

Here's an example of the list: [(34,23,54,34), (34,23,230,34),
(34,23,523,334), (34,23,15,17), (34,23,54,17), (45,23,43,123),
(564,23,543,23), (23,54,600,23), (34,54,23,654), (43,54,32,34)]

My first question is in regards to style first. The style guide for Python
doesn't seem to state this (if it has I missed it) but should I be doing
reverse=True or reverse = True with the spaces. lol this is so miniscule but
it's good to know. Also, does this function look/feel right to all the pros
out there. Is there a better way of doing it?

I'm still wrapping my head around ways of accomplishing proper sorts!

Dave
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081002/6870f21f/attachment.html>


More information about the Python-list mailing list