0

我的文本文件命名为content_data以下内​​容

A house is house that must be beautiful house and never regrets the regrets for the baloon in 
the baloons. Find the words that must be the repeated words in the file of house and ballons
  1. 现在我需要使用 python 读取文件并且需要找到每个单词的计数
  2. 我们需要以字典的形式实现结果,如下所示

    {'house':4,'baloon':3,'in':4........},

    我的意思是格式为{word:count}

谁能让我知道如何做到这一点

4

1 回答 1

1
from collections import Counter
from string import punctuation

counter = Counter()
with open('/tmp/content_data') as f:
  for line in f:
    counter.update(word.strip(punctuation) for word in line.split())

result = dict(counter)

# note: because we have
#   isinstance(counter, dict)
# you may as well leave the result as a Counter object

print result
于 2013-04-18T06:35:58.467 回答