Sorting a list

Peter Otten __peter__ at web.de
Mon Apr 4 02:56:26 EDT 2016


DFS wrote:

> On 4/3/2016 3:31 PM, Peter Otten wrote:

>>>>> from operator import itemgetter as get
>>>>> print "\n".join("{1} {0}".format(*p) for p in sorted(
>> ... sorted(colCounts, key=get(1)), key=get(0), reverse=True))
> 
> Kind of clunky looking.  Is that why don't you recommend it?

That, and you produce two intermediate lists and an intermediate string. For 
larger input data this may produce significant overhead.

>> You could also cheat and use
>>
>> lambda v: (-v[0], v[1])
>>
>> and a single sorted().
> 
> That works well.  Why is it 'cheating'?

On second thought it isn't ;)




More information about the Python-list mailing list