[Tutor] Sorting a list of lists aka nested lists
Kent Johnson
kent37 at tds.net
Sat Aug 20 04:08:35 CEST 2005
mailing list wrote:
> No-one's mentioned a Schwartzian Transform yet ; )
because it's obsoleted by the key parameter to sort...
>
>
>
> On 8/15/05, Kent Johnson <kent37 at tds.net> wrote:
>
>>jfouhy at paradise.net.nz wrote:
>>
>>>Note that in Python2.4+, you can use key= instead:
>>>
>>>def sortKey(lst):
>>> return lst[2]
>>>Quant.sort(key=sortKey)
>>>
>>>This is more effient than specifying a different comparison function, because
>>>the key function is only called once for each element.
>>
>>And instead of defining your own function you can use itertools.itemgetter() which is intended for this purpose:
>>import itertools
>>Quant.sort(key=itertools.itemgetter(2))
>>
>>Kent
>>
>>_______________________________________________
>>Tutor maillist - Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
>>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list