# 第 0006 题:你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。
import re
import glob
from collections import Counter
def create_list(file):
with open(file) as f:
f_list = []
for line in f:
text = re.sub('\"|\.|,',"",line) #去掉“ , .
line_list = text.strip().split(' ')
print(line_list)
f_list.extend(line_list) # list.extend() 往列表中一次添加多个元素
return f_list
# 使用collections.Counter
def get_count1(file):
f_list = create_list(file)
print(Counter(f_list))
print(Counter(f_list).most_common(1))
return Counter(f_list).most_common(1)
def txt_list():
return glob.glob('*.txt')
if __name__ == "__main__":
# c1 = get_count1('0004.txt')
s = list(map(get_count1,txt_list()))
print(s)
第 0006 题:你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。
猜你喜欢
转载自blog.csdn.net/qq_24822271/article/details/102609341
今日推荐
周排行