[Tutor] .sort(key = ???)
pythontut at pusspaws.net
Thu Nov 16 23:40:40 CET 2006
On Thursday 16 November 2006 22:35, John Fouhy wrote:
> On 17/11/06, Dave S <pythontut at pusspaws.net> wrote:
> > Hi,
> > I have a bunch of lists within lists that I need to sort ref item , I
> > can't access my code at the moment but I basically ...
> > Having googled I think there is a better way of doing this with the key
> > attribute
> > a.sort(key= ?? )
> Yep, that's right. You need at least python2.4 for this, though.
> You need to pass the key= parameter a function that will take an
> element of your list, and return the comparison key.
> def item4(elem):
> return elem
> Since this is a common thing to do, there is a function in the
> operator module that you can use, instead of defining your own:
> import operator
> (operator.itemgetter(4) will return a function that is basically
> equivalent to item4() above)
Sure does - cheers
More information about the Tutor