sorting a dictionary?
Anthony Liu
antonyliu2002 at yahoo.com
Mon Mar 21 22:04:08 EST 2005
--- Scott David Daniels <Scott.Daniels at Acm.Org> wrote:
> Anthony Liu wrote:
> > mydict = {'the':358, 'they':29, 'went':7,
> 'said':65}
> > Is there an easy to sort this dictionary and get a
> > list like the following (in decreasing order)?
> Yes.
>
> def sortkey((word, frequency)):
> return -frequency, word
>
> for word_freq in sorted(mydict.items(),
key=sortkey):
> print '%-6s %s' % word_freq
>
Thank you scott, but 'sorted' itself is not a python
library function. So I assume by
"sorted(mydict.items()", you meant the object of
mydict.items() which has been sorted, right?
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
More information about the Python-list
mailing list