Skip to content

Commit 1294d21

Browse files
author
Will
authored
finish No.0004
1 parent f6e9314 commit 1294d21

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

will/0004/sum.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 第 0004 题: 任一个英文的纯文本文件,统计其中的单词出现的个数。
2+
3+
import re
4+
path = './a.txt'
5+
6+
def count(data):
7+
words = re.compile('[a-zA-Z0-9]+')
8+
di = {}
9+
for i in words.findall(data):
10+
if i not in di:
11+
di[i] = 1
12+
else:
13+
di[i] += 1
14+
return di
15+
16+
if __name__ == '__main__':
17+
with open(path, 'r') as file:
18+
data = file.read().lower()
19+
sumofword = count(data)
20+
print(sumofword)
21+
file.close()

0 commit comments

Comments
 (0)