文件方式实现完整的英文词频统计实例

fo=open('123.txt','r')
a=fo.read()

a=a.lower() #小写
for i in ',.': 
    a=a.replace(i,' ')  #替换标点符号
words=a.split()  #分单词
print(words)

dic={} #字典
keys=set(words) #集合
print(keys)

for i in keys:
    dic[i]=words.count(i) #出现单词的次数
print(dic)

wc = list(dic.items()) #列表

wc.sort(key=lambda x:x[1],reverse=True)
print(wc)

for i in range(20):
    print(wc[i])
fo.close()

  1、

2、

3、

4、

5、

原文地址:https://www.cnblogs.com/Sun584125503/p/7594912.html