编写一个 Python 程序统计下面文本中出现最多的单词是什么?出现了多少次?(要求不区分大小写)
时间: 2024-05-08 09:16:35 浏览: 12
以下是一个 Python 程序,可以统计文本中出现最多的单词及其出现次数:
```python
text = """The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
"""
# 将文本转换为小写,并去除标点符号和换行符
text = text.lower().replace(",", "").replace(".", "").replace("\n", " ")
# 将文本拆分为单词列表
words = text.split(" ")
# 统计每个单词出现的次数
word_counts = {}
for word in words:
if word not in word_counts:
word_counts[word] = 1
else:
word_counts[word] += 1
# 找出出现次数最多的单词及其出现次数
most_common_word = ""
most_common_count = 0
for word, count in word_counts.items():
if count > most_common_count:
most_common_word = word
most_common_count = count
# 输出结果
print("出现最多的单词是 '%s',出现了 %d 次。" % (most_common_word, most_common_count))
```
输出结果为:
```
出现最多的单词是 'the',出现了 14 次。
```