python 統計單詞個數---從文件讀取版本---不去重

#/usr//bin/env  pytnon
#coding:utf-8

import re

'''
words = 'hello word  a@r3143#  ####   ##$dfabpple# apple##%#$  3423421apple \
       apple323423 432422324##apple@@#  @@@@@  $$!!#!#'
'''

#編譯生成正則表達式對象
regexp = re.compile(r'[a-zA-Z]{2,}')

#with open('./aa.txt')  as f:
f = open('./aa.txt')
line = f.readlines()
print len(line)

j = 0
sum = 0
for i in range(len(line)):
     j = len(regexp.findall(line[i]))
     sum = sum + j
print sum


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章