David (dblume) wrote,
David
dblume

Python Sorting

Python's sorting is extremely powerful and intuitive, so it seems foolish for me to post this note-to-self here, but I want to do so, in case I forget the operator module.

import operator

rows.sort(key=operator.itemgetter(4))
# or
rows.sort(lambda x, y : x[4] == y[4] and cmp(x[2],y[2]) or cmp(x[4], y[4]))

...not like I could just find the same info at the Python wiki or anything. :-P
Tags: programming, python
Subscribe

  • Progress on my Google+ and LJ backups

    Since Google is going to shut down Google+, I decided it was time to really make a home for my LiveJournal backup and my Google+ backup. Working…

  • Getting Shit Done

    I came across an old LifeHacker article Get Shit Done Blocks Distracting Web Sites So You Can Do As the Name Instructs, that mentions a productivity…

  • Fifth Grade Homework - Nine digit pandigital prime number

    Yesterday my daughter in the fifth grade got the following homework assignment "arrange the digits one through nine into a nine-digit prime number."…

  • Post a new comment

    Error

    Comments allowed for friends only

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments