输出样例: 5出现的次数为:12 33出现的次数为:55
时间: 2024-12-01 11:30:18 浏览: 5
要实现这个功能,你可以遵循以下步骤:
1. 输入文本并去除标点符号[^1]:
```python
import string
def remove_punctuation(text):
translator = str.maketrans("", "", string.punctuation)
return text.translate(translator)
# 示例输入
text = input().split()
words = [remove_punctuation(word) for word in text if word.isalpha()]
```
2. 统计每个单词的出现次数:
```python
word_count = {}
for word in words:
if word.lower() not in word_count:
word_count[word.lower()] = 1
else:
word_count[word.lower()] += 1
sorted_word_count = sorted(word_count.items(), key=lambda x: (-x[1], x[0]))
```
3. 按照题目要求排序并输出结果:
```python
for word, count in sorted_word_count:
print(f"{word}出现的次数为:{count}")
```
这样,你就可以得到按词频降序和字母升序排列的单词及其出现次数。
阅读全文