Learning Python via a little word frequency program

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Thu Jan 10 03:47:28 EST 2008


Paul Hankin a écrit :
> On Jan 9, 12:19 pm, Bruno Desthuilliers <bruno.
> 42.desthuilli... at wtf.websiteburo.oops.com> wrote:
>> Andrew Savige a écrit :
>>> and the -x hack above to
>>> achieve a descending sort feels a bit odd to me, though I couldn't think
>>> of a better way to do it.
>> The "other" way would be to pass a custom comparison callback to sort,
>> which would be both slower and more complicated. Your solution is IMHO
>> the right thing to do here.
> 
> Both list.sort and sorted have a parameter 'reverse' which does a
> descending rather than ascending sort.

Yes. But here the specs says that sort must be descending on first key 
(frequency) and ascending on the second (name), so you can't just use 
reverse sort. FWIW, a correct (and readable) solution - based on the 
'key' param - has been given by Peter Otten, but it still requires a 
callback function, so while it avoids the '-x hack', it's  still more 
expensive than a plain sort.





More information about the Python-list mailing list