Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin1!goblin.stu.neva.ru!news.astraweb.com!border5.a.newsrouter.astraweb.com!bcyclone02.am1.xlned.com!bcyclone02.am1.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '#print': 0.09; 'filename': 0.09; 'fname': 0.09; 'function:': 0.09; 'occurrences': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'val': 0.09; 'def': 0.12; 'count,': 0.16; 'dict': 0.16; 'increment': 0.16; 'pairs': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sorting': 0.16; 'subject:top': 0.16; 'skip:# 20': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'bit': 0.19; "skip:' 30": 0.19; 'split': 0.19; 'subject:Issue': 0.19; 'written': 0.21; 'print': 0.22; 'header :User-Agent:1': 0.23; 'simpler': 0.24; 'sort': 0.25; 'first,': 0.26; 'header:X-Complaints-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'skip:p 30': 0.29; 'words': 0.29; 'characters': 0.30; 'skip:( 20': 0.30; 'gives': 0.31; 'keys': 0.31; 'values.': 0.31; 'file': 0.32; 'figure': 0.32; "can't": 0.35; 'something': 0.35; 'convert': 0.35; 'late': 0.35; 'but': 0.35; 'possible': 0.36; 'effort': 0.37; 'too': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'fact': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'first': 0.61; 'such': 0.63; 'total': 0.65; 'here': 0.66; 'reverse': 0.68; 'apart': 0.72; 'subject:printing': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Issue in printing top 20 dictionary items by dictionary value Date: Sat, 04 Oct 2014 12:20:07 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bda5c6.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1412418025 news.xs4all.nl 2974 [2001:888:2000:d::a6]:55924 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 5057 X-Received-Body-CRC: 1284914455 Xref: csiph.com comp.lang.python:78687 Shiva wrote: > Hi All, > > 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: > > def print_top(filename): > > #Open a file > path = '/home/BCA/Documents/LearnPython/ic/' > fname = path + filename > print ('filename: ',fname) > filetext = open(fname) > > #Read the file > textstorage={} > > #print(type(textstorage)) > readall = filetext.read().lower() > eachword = set(readall.split()) > > #store split words as keys in dictionary > for w in eachword: > textstorage[w] = readall.count(w) Using count() here is very inefficient. A better approach is to increment the dict value: for w in readall.split(): textstorage[w] = textstorage.get(w, 0) + 1 > > #print top 20 items in dictionary by decending order of val > # This bit is what I can't figure out. > > for dkey in (textstorage.keys()): > print(dkey,sorted(textstorage[dkey]))?? Apart from the fact that you are sorting characters in the word at that point the sorting effort is already too late -- you need to sort the dict keys by the corresponding dict values. It is possible to write a get_value() function such that sorted(textstorage, key=get_value, reverse=True) gives the keys in the right order, but perhaps it is simpler to convert textstorage into a list of (count, word) pairs first, something like pairs = [(42, "blue"), (17, "red"), (77, "black"), ...] When you sort that list most_common_words = sorted(pairs, reverse=True) you automatically get (count, word) pairs in the right order and can print the first 20 with for count, word in most_common_words[:20]: print(word, count) PS: Once you have it all working have a look at collections.Counter...