1

我想将文本文件中出现的每个单词的计数存储在字典中。我是说

fob= open('D:/project/report.txt','r')

我可以将这些行存储到一个列表中,但我需要将这些行拆分为单个单词并最终存储它们的计数(就像在字典中一样)。

lst=fob.radlines()

#This doesn't work, gives error
#AttributeError: 'list' object has no attribute 'split' 
mylst=lst.split()

我怎样才能做到这一点 ?这样做的有效方法是什么?

4

1 回答 1

1

对于 Python2.7+

from collections import Counter

with open('D:/project/report.txt','r') as fob:
    c = Counter(word for line in fob for word in line.split())

对于 Python2.5+

from collections import defaultdict
dd = defaultdict(int)

with open('D:/project/report.txt','r') as fob:
    for line in fob:
        for word in line.split():
            dd[word] += 1

对于年长的蟒蛇或讨厌的人defaultdict

d = {}

with open('D:/project/report.txt','r') as fob:
    for line in fob:
        for word in line.split():
            d[word] = d.get(word, 0) + 1
于 2013-04-01T03:17:42.443 回答