Python: re

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

import re
from collections import defaultdict


regex = re.compile(r'[^\w-]+')
valor = defaultdict(lambda: 0)

with open(file = 'statistic.txt', mode = 'r+t', encoding = 'utf8', errors = 'strict', newline = None) as f:
    for line in f:
        for word in regex.split(line):
            if word and not word.isdigit():
                valor[word.lower()] += 1

valor = sorted(valor.items(), key = lambda item: item[1], reverse = True)
print(valor)

 

posted @ 2022-03-02 23:45  ascertain  阅读(20)  评论(0编辑  收藏  举报