User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 426,248 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,092 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums

How to sort word (from file) frequancy in decrease order? I need help

Join Date: Mar 2008
Posts: 16
Reputation: alivip is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
alivip alivip is offline Offline
Newbie Poster

How to sort word (from file) frequancy in decrease order? I need help

  #1  
Mar 14th, 2008
I wont to find most 10 frequency word of specific file for that I have written this code


import sys
import string
import re
file = open ( "corpora.txt", "r" )
text = file.read ( )
file.close ( )

word_freq ={ }

word_list = string.split ( text )

for word in word_list:
    count = word_freq.get ( string.lower ( word ), 0 )
    word_freq[string. lower ( word )] = count + 1

keys = word_freq.keys ( )
keys.sort ( )
i=0
while i<10:
 for word in keys:
     
     print word, word_freq[word]
     i=1+1




but it only get the word and its frequency
sample output

blanklines 2
blanklines, 1
characters 1
console 1
count 3
blanklines 2
blanklines, 1
characters 1
console 1
count 3

and its read the file again and again. also it did not sort the
output as you can see

how I can sort output in decrease order to be able to stop print after 10 words?

please help me ASAP
AddThis Social Bookmark Button
Reply With Quote  
All times are GMT -4. The time now is 8:45 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC