#统计文档中每个字出现的次数
word_count={}
with open("D:\Desktop\wde.txt")as fin:#打开文档
for line in fin :#提取文档的内容
line=line[:-1]#去掉最后的换行符
w=line.split()#单词之间是空格
for word in w:#提取文档内容
if word not in word_count:#如果没有见过计0
word_count[word]=0
word_count[word] +=1#已经计过的加1
w_sort = sorted(
word_count.items(),#返回一个列表,将每个键值对分开,键与值之间用逗号分割
key=lambda x: x[1],
reverse=True
)[:10]#只返回成绩排前10的数据
print(w_sort)#[('and', 11), ('is', 9), ('face', 4), ('can', 4),
print(word_count.items())#dict_items([('Everybody', 1), ('has', 1), ('to', 3), ('face', 4), ('bad', 3),
print(word_count)#{'Everybody': 1, 'has': 1, 'to': 3, 'face': 4, 'bad': 3, 'days': 3,
标签:count,word,face,单词,次数,文档,print,line
From: https://www.cnblogs.com/36zy/p/16990978.html