词频统计:统计python之禅中每个单词出现的次数,并按次数进行降序排列输出。
时间: 2024-09-27 17:05:47 浏览: 50
统计单词在文章中出现频率
3星 · 编辑精心推荐
在Python中,你可以使用`collections`模块的`Counter`类来进行词频统计。首先,需要将文本拆分成单词列表,然后创建一个`Counter`对象来计算每个单词的频率,最后按照频率排序并输出结果。这里是一个简单的步骤示例:
```python
import re
from collections import Counter
# Python之禅内容
zen_of_python = """
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""
# 使用正则表达式分割成单词列表
words = re.findall(r'\b\w+\b', zen_of_python.lower())
# 使用Counter计算词频并降序排序
word_counts = Counter(words).most_common()
# 输出结果
for word, count in word_counts:
print(f"{word}: {count}")
```
阅读全文