编写程序对输入的英文文本进行统计,统计出现频率最高的5个单词组,同频组内的单词全小写按升序输出。单词是指连续的英文和数字字符。 测试1:(第1部分:输入内容可能有多段,最后一段以三个$$$为输入结束标志。第2部分:输出包含5行) The Sick Lion A Lion had come to the end of his days and lay sick unto death at the mouth of his cave, gasping for breath. The animals, his subjects, came round him and drew nearer as he grew more and more helpless. When they saw him on the point of death they thought to themselves: "Now is the time to pay off old grudges." So the Boar came up and drove at him with his tusks; then a Bull gored him with his horns; still the Lion lay helpless before them: so the Ass, feeling quite safe from danger, came up, and turning his tail to the Lion kicked up his heels into his face. "This is a double death," growled the Lion. Only cowards insult dying majesty. $$$ 11:['the'] 8:['his'] 5:['and', 'lion'] 4:['him', 'to'] 3:['a', 'came', 'death', 'of', 'up']
时间: 2024-03-09 13:49:14 浏览: 124
以下是 Python 代码实现:
```python
import re
from collections import defaultdict
# 读入文本
text = ""
while True:
line = input()
if line == "$$$":
break
text += line + " "
# 统计单词出现次数
word_freq = defaultdict(int)
words = re.findall(r'\w+', text.lower())
for word in words:
word_freq[word] += 1
# 按出现次数排序,同频组内按字母升序排序
top5 = sorted(word_freq.items(), key=lambda x: (-x[1], x[0]))[:5]
for freq, group in [(freq, [w for w in word_freq if word_freq[w]==freq]) for freq in sorted(set(word_freq.values()), reverse=True)[:5]]:
group.sort()
print(f"{freq}: {group}")
```
输出:
```
11: ['the']
8: ['his']
5: ['and', 'lion']
4: ['him', 'to']
3: ['a', 'came', 'death', 'of', 'up']
```
相关问题
Python,编写程序对输入文本进行统计,统计出现频率最高的5个单词组,同频组内的单词全小写按升序输出
好的,我可以帮你完成这个任务。你可以使用Python的collections模块中的Counter类来统计单词出现的频率,然后使用sorted函数对结果进行排序,最后输出出现频率最高的5个单词组。
下面是示例代码:
```python
from collections import Counter
text = input("请输入需要统计的文本:")
# 将文本中的标点符号替换为空格,并将所有单词转换为小写
words = text.lower().replace(",", " ").replace(".", " ").split()
# 统计单词出现次数
word_counts = Counter(words)
# 按出现次数从大到小排序
top_words = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)[:5]
# 遍历同频组,按升序输出
for count in range(top_words[0][1], 0, -1):
group = sorted(word[0] for word in top_words if word[1] == count)
print(", ".join(group))
```
你可以将文本输入程序中,程序会输出出现频率最高的5个单词组,同频组内的单词全小写按升序输出。
希望能帮到你!
用pycharm编写一个程序实现如下功能:读入一个文本文件,统计在该文本文件中每个英文单词出现的频率,并输出单词频率最 高的 100 个单词。其中,单词的定义是连续的若干个小写英文字母。
好的,我明白了。以下是实现该功能的 Python 代码:
```python
import re
from collections import Counter
# 打开文本文件并读取
with open('text_file.txt', 'r') as f:
text = f.read()
# 使用正则表达式匹配连续的小写英文字母
words = re.findall(r'\b[a-z]+\b', text.lower())
# 统计单词出现的频率
word_counts = Counter(words)
# 打印出现频率最高的 100 个单词
for word, count in word_counts.most_common(100):
print(word, count)
```
其中,`text_file.txt` 是要统计的文本文件名。该程序使用正则表达式匹配单词,忽略大小写,并使用 `collections` 模块的 `Counter` 类来统计单词出现的频率。最后打印出现频率最高的 100 个单词及其出现次数。
阅读全文