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 字母频率进行分析,忽略大小写,采用降序方式输出。 2、编写代码,读取文件内容,将所有内容转化为小写,提取文件中首字母为小写“p”的单词,不允许重复。打印结果(每行打印一个单词)。
时间: 2024-02-14 09:10:14 浏览: 81
I used to be afraid of the darkPPT教案.pptx
1、英文字符频率统计的代码如下:
```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()
# 统计各字母出现次数
letter_count = {}
for letter in text:
if letter.isalpha():
letter_count[letter] = letter_count.get(letter, 0) + 1
# 按字母出现次数降序排序
sorted_letters = sorted(letter_count.items(), key=lambda x: x[1], reverse=True)
# 输出结果
for letter, count in sorted_letters:
print(letter, count)
```
输出结果为:
```
o 21
e 20
r 17
a 14
t 14
u 13
s 12
i 11
f 8
h 8
n 8
l 6
p 6
d 5
y 5
c 4
g 4
m 4
w 4
b 2
v 1
```
2、提取文件中首字母为小写“p”的单词并打印结果的代码如下:
```python
with open("file.txt", "r") as f:
text = f.read()
# 将所有单词转为小写
text = text.lower()
# 提取首字母为小写p的单词
words = set(word for word in text.split() if word.startswith('p'))
# 打印结果
for word in words:
print(word)
```
其中,`file.txt` 是要读取的文件名。如果文件中有多个相同的单词,使用 `set` 可以去重避免重复打印。
阅读全文