forked from lymin/python_interview_question
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31.py
More file actions
30 lines (24 loc) · 846 Bytes
/
31.py
File metadata and controls
30 lines (24 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import re
def word_fre(filepath):
fre = {}
with open(filepath) as f:
for line in f:
line = re.sub('\W+', ' ', line) # \W匹配任意非数字和字母字符,都将其替换为空格
print(line)
words = line.split()
for word in words:
if not fre.get(word):
fre[word] = 1
else:
fre[word] +=1
num_ten = sorted(fre.items(), key=lambda x:x[1], reverse=True)[:10]
num_ten = [x[0] for x in num_ten]
return num_ten
from collections import Counter
def word_fre2(filepath):
with open(filepath) as f:
return list(map(lambda x: x[0], Counter(re.sub('\W+',
' ', f.read()).split()).most_common(10)))
if __name__ == '__main__':
filepath = "31.txt"
print(word_fre2(filepath))