[Newbie] How to output dictionary sorted on content (not keys)

Achim Domma achim.domma at syynx.de
Thu Jul 4 08:37:15 EDT 2002


not tested, but something like this should work:

//    can this be done with lambda ???
def myComp(a,b): return cmp(a[1],b[1])
tmpList = yourDict.items()
tmpList.sort(myComp)

for name,count in tmpList: print name,count

Achim


"Ben Fairbank" <baf at texas.net> wrote in message
news:3d243af0.465960 at news.texas.net...
> I have several long (thousands of entries) dictionaries of words used
> in plays; associated with each word is the number of times it is used
> in the play.  I have had no problem printing the words alphabetically
> (send keys to a list, sort the list, access dictionary by the sorted
> key list), but I have not found an elegant way to sort by  frequency
> of use.  The best I have been able to do is a workable but inelegant
> kluge that looks more like Basic than Python.  Is there a "Python Way"
> of doing that?
>
> Thanks for any ideas.
>
> BAF





More information about the Python-list mailing list