英文
from collections import Counter
with open(textinfo,'rb') as fp:
data=fp.readlines()
text=[line.strip().lower() for line in data]
cnt=Counter()
for word in text:
cnt[word]+=1
cnt=dict(cnt)
sorted(cnt.items(),key = lambda x:x[1],reverse = True) #排序
for key,value in cnt.items():
print(key+":"+str(value))
中文
from collections import Counter
with open(textinfo,'rb') as fp:
data=fp.read()
text=unicode(data,"gbk")
cnt=Counter()
for word in text:
cnt[word]+=1
cnt=dict(cnt)
sorted(cnt.items(),key = lambda x:x[1],reverse = True) #排序
for key,value in cnt.items():
print(key+":"+str(value))