擅长:python、mysql、java
<p>下面是一个答案,它使用列表理解来获得你的示例文本中单词的频率。你知道吗</p>
<p>另外,可以修改此代码以使用CSV文件。你知道吗</p>
<pre><code>from pprint import pprint
input_text = """
I have 12 trampolines. The TRAMPOLINES are round and have netting
surrounding them.
Trampolines are my favorite activity.
"""
wordfreq = Counter([word.lower().rstrip('.') for word in input_text.split()])
pprint (wordfreq)
# outputs
Counter({'trampolines': 3,
'have': 2,
'are': 2,
'i': 1,
'12': 1,
'the': 1,
'round': 1,
'and': 1,
'netting': 1,
'surrounding': 1,
'them': 1,
'my': 1,
'favorite': 1,
'activity': 1})
</code></pre>