python词频统计
时间: 2023-07-01 09:26:24 浏览: 91
您可以使用Python中的collections库来实现词频统计。以下是一个简单的例子:
```python
import collections
text = "Python is an interpreted high-level programming language for general-purpose programming. Created by Guido van Rossum and first released in 1991, Python has a design philosophy that emphasizes code readability, notably using significant whitespace. It provides constructs that enable clear programming on both small and large scales."
words = text.lower().split() # 将文本转换为小写并按空格分割成单词列表
word_counts = collections.Counter(words) # 统计单词出现的次数
print(word_counts)
```
输出结果为:
```
Counter({'programming': 2, 'python': 2, 'and': 2, 'is': 1, 'an': 1, 'interpreted': 1, 'high-level': 1, 'language': 1, 'for': 1, 'general-purpose': 1, 'created': 1, 'by': 1, 'guido': 1, 'van': 1, 'rossum': 1, 'first': 1, 'released': 1, 'in': 1, '1991,': 1, 'has': 1, 'a': 1, 'design': 1, 'philosophy': 1, 'that': 1, 'emphasizes': 1, 'code': 1, 'readability,': 1, 'notably': 1, 'using': 1, 'significant': 1, 'whitespace.': 1, 'it': 1, 'provides': 1, 'constructs': 1, 'enable': 1, 'clear': 1, 'on': 1, 'both': 1, 'small': 1, 'large': 1, 'scales.': 1})
```
其中,Counter对象是一个字典,包含每个单词出现的次数。
阅读全文