Learning Python via a little word frequency program
Paul Hankin
paul.hankin at gmail.com
Wed Jan 9 18:56:13 EST 2008
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.
eg.
deco.sort(reverse=True)
for v, k in deco:
...
or
for v, k in sorted(deco, reverse=True):
...
--
Paul Hankin
More information about the Python-list
mailing list