统计英文单词次数

方法一
from collections import Counter

import re

with open('../one_one.py',encoding="utf-8",)as f:
f1 = f.read()
rest = re.findall(r'[a-zA-Z].+',f1)

res = Counter(rest)
print(res)
for k,l in res.items():
print("%s:%s" % (k,l))

方法二
import re
with open('../one_one.py',encoding="utf-8", )as f:
f1 = f.read()
f2 = re.sub(r"[.?!,()/#:'=->]", ' ', f1)
res = f1.split()
print(res)
dict1 = {}
for k in res:
if re.findall(r'[a-zA-Z]',k):
ress = res.count(k)
dict1[k] = ress
print(dict1)
ll = sorted(dict1.items(),key=lambda x:x[1],reverse=True)
print(ll)

猜你喜欢

转载自www.cnblogs.com/zxt-cn/p/9714894.html