|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
How to sort word (from file) frequancy in decrease order? I need help
I wont to find most 10 frequency word of specific file for that I have written this code
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 |
![]() |
| Viewing: Web Development Archives > Mailing Lists > Python > How to sort word (from file) frequancy in decrease order? I need help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|