re模块进行单词统计
2023-09-11 14:16:16 时间
from collections import defaultdict
import re
d=defaultdict(lambda :0)
with open(r'e:/bb.txt',mode='rt+',encoding='utf8') as f:
for line in f:
for sub in re.split('[^\w-]+',line):
if len(sub)>0:
d[sub]+=1
b=0
for p in sorted(d,key=lambda x:d[x],reverse=True):
if b<10:
print(p,d[p])
b+=1
b=0
for p in sorted(d.items(),key=lambda x:x[1],reverse=True):
if b<10:
print(p[0],p[1])
b+=1
import re
from collections import defaultdict
d=defaultdict(lambda :0)
regex=re.compile('[^\w-]+',flags=re.S|re.I)
with open('e:/bb.txt',mode='rt+',encoding='utf8') as f:
for line in f:
for sub in regex.split(line):
if len(sub)>0:
d[sub.lower()]+=1
print(d)
v=0
for p in sorted(d,key=lambda u:d[u],reverse=True):
if v<10:
print(p,d[p])
v+=1
def wordcount(path:str='.'):
d=defaultdict(lambda :0)
with open(path,mode='rt+',encoding='utf8') as f:
for line in f:
for sub in regex.split(line):
if len(sub)>0:
d[sub.upper()]+=1
return d
v=0
for p in sorted(wordcount('e:/bb.txt').items(),key=lambda m:m[1],reverse=True):
if v<10:
print(p)
v+=1
相关文章
- Android 10系统下的PSS数值统计不准
- 【算法】【字符串模块】字符串的统计字符串和通过统计字符串查找字符
- R语言中的四类统计分布函数
- Google Earth Engine(GEE)——ee.List 列表初始化,序列分析,添加、合并、删减、替换、判断、排序、反转、去重,统计和循环遍历计算
- 地球引擎中级教程——reducer统计的运用(含练习)
- 怎么样记录、统计、测量出Python程序的运行时间(用Python内置的datetime实现)
- 前端监控之用户行为监控实践2(数据统计mongodb)
- 自定义百度统计功能使用帮助文档
- 基于Python+SQLite实现校园信息化统计平台【100010095】
- 力扣解法汇总2409. 统计共同度过的日子数
- 统计单词数 OpenJ_Bailian - 4030 (KMP)
- 字符串统计
- 统计学习的基本方法步骤
- nginx自定义模块编写-实时统计模块--转载
- CAD如何统计多个数字求和?CAD统计求和命令应用技巧