Python实现的wordcount的小程序

Python实现的wordCount程序。
读入一个文本文件,统计单词出现的次数。
直接贴代码如下:
f = open("words.txt","r")
lines = [line for line in f]
words = []
wordsCount = {}
for line in lines:
        for word in line.split(' '):
                words.append(word)
for word in words:
        wordsCount[word] = wordsCount.get(word,0) + 1
print wordsCount

0 个评论

要回复文章请先登录注册