[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Learning Python via a little word frequency program

Fredrik Lundh

1/9/2008 12:14:00 PM

Andrew Savige wrote:

> Neat. Is there any way to use sorted() with multiple sort keys? ...

sure! just return the "composite key" as a tuple:

# sort on descending count, ascending name
deco = sorted(freq.items(), key=lambda x: (-x[1], x[0]))

(when comparing tuples, Python first compares the first item from each
tuple. if they're equal, it continues with the second item, and so on).

</F>