Skip to content

Commit fe65912

Browse files
committed
Merge pull request Show-Me-the-Code#107 from lulujianjie/master
Finished 0004 with comments in source file
2 parents 94a3172 + 8d33003 commit fe65912

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

lulujianjie/0004/0004.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import re
2+
lines_count = 0
3+
#words_count = 0
4+
chars_count = 0
5+
lines_list = []
6+
words_list = {}
7+
with open('./test.txt','r') as f:
8+
#http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386820066616a77f826d876b46b9ac34cb5f34374f7a000
9+
for line in f:## 使用文件迭代器 , 每次只读取和显示一行
10+
#http://blog.csdn.net/mvpme82/article/details/5674200
11+
lines_count += 1
12+
chars_count += len(line)#算空格
13+
#正则表达式http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832260566c26442c671fa489ebc6fe85badda25cd000
14+
lines_list = re.findall(r'[a-zA-Z\-]+',line)
15+
print lines_list
16+
for i in lines_list:
17+
#words_count += 1
18+
if i not in words_list:
19+
words_list[i] = 1
20+
else:
21+
words_list[i] += 1
22+
23+
print 'lines_count:',lines_count
24+
print 'chars_count:',chars_count
25+
print 'words_count:',len(words_list)#输出字典的元素个数
26+
27+
for word,quantity in words_list.items():
28+
print word,'\'s number is ',quantity
29+
## f.close()

lulujianjie/0004/test.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
hello world!
2+
lujianjie
3+
###
4+
ksdfksdkk*&
5+
ljj-wyj
6+
7+
lujianjie
8+
###
9+
ksdfksdkk*&
10+
ljj-wyj lujianjie
11+
###
12+
ksdfksdkk*&
13+
ljj-wyj

0 commit comments

Comments
 (0)