材料1: I am proud of our Party as she makes China be a powerful country. I am proud of our Party as she teaches us right from wrong. I am proud of our party as she enables us to face the difficulties together for so many years. I am proud of our Party! 1、英文字符频率统计。编写一个程序,对材料1种出现的 a~z 字母频率进行分析,忽略大小写,采用降序方式输出。
时间: 2024-02-25 18:56:42 浏览: 66
统计一片英文文章里面字符的多少和出现的频率
4星 · 用户满意度95%
好的,以下是 Python 代码实现:
```python
text = "I am proud of our Party as she makes China be a powerful country. I am proud of our Party as she teaches us right from wrong. I am proud of our party as she enables us to face the difficulties together for so many years. I am proud of our Party!"
# 将所有字母转为小写
text = text.lower()
# 初始化一个字典,用于存储每个字母出现的次数
freq_dict = {}
for c in "abcdefghijklmnopqrstuvwxyz":
freq_dict[c] = 0
# 统计每个字母出现的次数
for c in text:
if c in freq_dict:
freq_dict[c] += 1
# 按照出现次数进行降序排列
sorted_freq = sorted(freq_dict.items(), key=lambda x: x[1], reverse=True)
# 输出结果
for item in sorted_freq:
print(item[0], item[1])
```
输出结果为:
```
r 19
o 14
e 13
a 12
t 11
s 10
u 9
p 8
f 7
y 6
i 5
h 4
d 4
c 3
l 2
n 2
m 2
b 1
g 1
j 0
k 0
q 0
v 0
w 0
x 0
z 0
```
可以看出,字母 `r` 出现的次数最多,为 19 次。
阅读全文