Issue in printing top 20 dictionary items by dictionary value

Denis McMahon denismfmcmahon at gmail.com
Sat Oct 4 21:11:40 EDT 2014


On Sat, 04 Oct 2014 09:11:43 +0000, Shiva wrote:

> I have written a function that -reads a file -splits the words and
> stores it in a dictionary as word(key) and the total count of word in
> file (value).
> 
> I want to print the words with top 20 occurrences in the file in reverse
> order - but can't figure it out. Here is my function:

Once you've generated your dictionary, use a list comprehension to turn 
it into a list of tuples of ( word, count ). Sort the list of tuples 
according to the count element. Select the top 20. copy it to a new list 
and reverse that list. now loop through the second list and print results.

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list