Python实现统计不同字符数量
发布时间
阅读量:
阅读量
问题引入:

问题解决:
1.用re模块
import re
d = {}
with open("E:\sample.txt", encoding = "utf-8") as f:
for line in f:
tmp = line.lower().strip() # 对一行字符串进行小写转换并切割两头空白符
lst = re.split('[ .(),=:"»~>*]', tmp)
for s in lst:
# if s not in d.keys():
# d[s] = 1
# else:
# d[s] += 1
d[s] = d.get(s, 0) + 1 # 经改进
print(sorted(d.items(), key=lambda x:x[1], reverse=True)) # 利用
全部评论 (0)
还没有任何评论哟~
