best Pythonic way to do this sort: Python newb

George Sakkis gsakkis at rutgers.edu
Mon Oct 10 09:29:58 EDT 2005


"Satchidanand Haridas" <sharidas at zeomega.com> wrote:

> >So, I want to sort myList by the return of myFunction( value3 )
> >
> >I tried doing the following... with no luck so far
> >myList.sort(lambda x, y: cmp(myFunction(x[2]), myFunction(y[2]))
> >
> >
> >
> I think the above statement should be as follows:
>
> myList.sort(lambda x, y: cmp(myFunction(x[2]) - myFunction(y[2]))
>
>
>
> hope that helps.

It would help more if you tested it before you posted. cmp takes two arguments (let alone that
subtraction may not be defined for the list elements), so the original version is correct.

George





More information about the Python-list mailing list