#统计一个文本中单词频次最高的10个单词?
import re
class Solution():
def MaxWord(self,file_name):
"""
:param file_name: 文件名
:return:
"""
with open(file_name,'r') as file:
for lines in file:
lines=re.sub('\W+',' ',lines)
print(lines)
dict_word={}
for i in range(0,len(lines.split(' '))):
word=lines.split(' ')[i]
if word not in dict_word:
dict_word[word]=1
else:
dict_word[word]+=1
word_sorted=sorted(dict_word.items(),key=lambda x:x[1],reverse=True)
for i in range(0,10):
print(word_sorted[i])
if __name__=='__main__':
solution=Solution()
file_name='E:\python_test\exercise0406\information.txt'
solution.MaxWord(file_name)