首页 > 试题广场 >

请你回答一下:统计文本中出现次数前十的单词,文件很大,不能一

[问答题]

请你回答一下:统计文本中出现次数前十的单词,文件很大,不能一次性读入内存?

import re from collections import Counter with open('1.txt', 'r', ) as f:
    words = f.read() # 将文件的内容全部读取成一个字符串 count = Counter(re.split(r"\W+", words)) # 以单词为分隔 result = count.most_common(10) # 统计最常使用的前10个 print(result)
发表于 2020-10-07 20:45:21 回复(0)