A statistics text word frequency up to 10 words?

# Statistics text in a word the highest frequency of 10 words? 

Import Re
 Import String 

WORD_LIST = [] 
word_dict = {} 
with Open ( " D: \\ 2.py " , " R & lt " ) AS FP: 
    fp_file = fp.readlines ()
     for Line in fp_file:
         IF line.strip () ! = '' : 
            line_word = the re.findall (R & lt " [a-zA-the Z] + " , Line)   # L Word microtome to take out, a return is List 
            WORD_LIST + = line_word    #The Word aggregated into a List 

for Word in WORD_LIST:
     IF Word in word_dict: 
        word_dict [Word] + =. 1
     the else : 
        word_dict [Word] =. 1 # dictionary sorted by value, and removing the first 10 data 
sorted_word_dict = sorted (word_dict .items (), Key = the lambda X: X [. 1], Reverse = True) [: 10 ] Print (sorted_word_dict)



 

Guess you like

Origin www.cnblogs.com/ff-gaofeng/p/11424281.html