1. 程式人生 > >文件方式實現完整的英文詞頻統計實例

文件方式實現完整的英文詞頻統計實例

() item .com div lambda 英文 [1] 完整 set

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、技術分享

文件方式實現完整的英文詞頻統計實例