Skip to content

Commit 218ea26

Browse files
committed
add 0006
1 parent 3cf2a0a commit 218ea26

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

Jaccorot/0006/0006.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,31 @@
77
"""
88

99
import os
10-
10+
import re
1111

1212
def walk_dir(path):
13-
for root, dirs, files in os.walk(path):
14-
for f in files:
15-
if f.lower().endswith('txt'):
16-
13+
file_path = []
14+
for root, dirs, files in os.walk(path):
15+
for f in files:
16+
if f.lower().endswith('txt'):
17+
file_path.append(os.path.join(root, f))
18+
return file_path
19+
20+
def find_key_word(filepath):
21+
word_dic = {}
22+
filename = os.path.basename(filepath)
23+
with open(filepath) as f:
24+
text = f.read()
25+
words_list = re.findall(r'[A-Za-z]+', text.lower())
26+
for w in words_list:
27+
if w in word_dic:
28+
word_dic[w] += 1
29+
else:
30+
word_dic[w] = 1
31+
sorted_word_list = sorted(word_dic.items(), key=lambda d: d[1])
32+
print u"在%s文件中,%s为关键词,共出现了%s次" %(filename, sorted_word_list[-1][0], sorted_word_list[-1][1])
33+
34+
if __name__ == "__main__":
35+
for file_path in walk_dir(os.getcwd()):
36+
find_key_word(file_path)
37+

Jaccorot/0006/1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a b c d a a d

Jaccorot/0006/2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a b c d a a d d b d d x zx fd fd fd fd fd fd fd fd fd FD FD FD FD D FD FD FD FD fD FD FD FD

0 commit comments

Comments
 (0)