题目 编写一个程序来计算输入的英文句子中单词的频率。 按字母顺序对键进行排序后输出。不统计各种标点符号:!@#$%^&*,;:'?{[]/}. 输入样例:God is impartial , the people who has destiny in hand stands forever in thebalance both ends , the people who is grasped by destiny knows that God best ows destiny on him only. 输出 应该是:每行输出一个词 及其出现次数,用冒号分隔 God:2 best:1 both:1 by:1 destiny:3 ends:1 forever:1 grasped:1 hand:1 has:1 him:1 impartial:1 in:2 is:2 knows:1 on:1 only.:1 ows:1 people:2 stands:1 that:1 the:2 thebalance:1 who:2
时间: 2023-12-19 19:04:58 浏览: 111
以下是 Python 代码实现:
```python
import string
# 读入句子并去除标点符号
sentence = input().translate(str.maketrans("", "", string.punctuation))
# 将句子按照空格分割成单词
words = sentence.lower().split()
# 统计单词频率
freq = {}
for word in words:
if word in freq:
freq[word] += 1
else:
freq[word] = 1
# 按照字母顺序对单词进行排序并输出
for word in sorted(freq.keys()):
print("{}:{}".format(word, freq[word]))
```
输入样例:
```
God is impartial , the people who has destiny in hand stands forever in the balance both ends , the people who is grasped by destiny knows that God bestows destiny on him only.
```
输出样例:
```
God:2
balance:1
bestows:1
both:1
by:1
destiny:3
ends:1
forever:1
grasped:1
hand:1
has:1
him:1
impartial:1
in:2
is:2
knows:1
on:1
only:1
people:2
stands:1
that:1
the:2
thebalance:1
who:2
```
阅读全文