Most Effective Way to Build Up a Histogram of Words?

Stephen Kloder stephenk at cc.gatech.edu
Thu Oct 12 14:40:46 EDT 2000


June Kim wrote:

>
> and then how could I sort the dictionary according to the
> frequency order?
>

Rather than creating a list-of-tuples data structure, you might want to use
Python's built-in sorting features:
words=histogram.keys()
words.sort(lambda x,y:cmp(histogram[y],histogram[x]))
for w in words:
    print "%s : %d"%(w,histogram[w])

The above code sorts in decreasing order.  Use cmp(histogram[x],histogram[y])
to sort increasing.

--
Stephen Kloder               |   "I say what it occurs to me to say.
stephenk at cc.gatech.edu       |      More I cannot say."
Phone 404-874-6584           |   -- The Man in the Shack
ICQ #65153895                |            be :- think.





More information about the Python-list mailing list