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. 1、英文字符频率统计。编写一个程序,对材料1种出现的单词频率进行分析,忽略大小写,采用降序方式输出。 2、编写代码,读取文件内容,将所有内容转化为大写,提取文件中首字母为大写“T”的单词,不允许重复。打印结果(每行打印一个单词)。
时间: 2024-03-09 10:46:39 浏览: 66
对于问题一,以下是Python代码实现:
```python
import re
from collections import Counter
with open('material.txt', 'r') as f:
text = f.read()
# 将文本转为小写,去除标点符号
text = re.sub('[^a-zA-Z ]+', '', text).lower()
# 统计单词频率
word_count = Counter(text.split())
# 按照降序输出单词频率
for word, count in sorted(word_count.items(), key=lambda x: x[1], reverse=True):
print(word, count)
```
对于问题二,以下是Python代码实现:
```python
import re
with open('material.txt', 'r') as f:
text = f.read()
# 将文本转为大写
text = text.upper()
# 提取以大写T开头的单词
words = re.findall(r'\bT\w*\b', text)
# 去除重复的单词
unique_words = set(words)
# 打印结果
for word in unique_words:
print(word)
```
这两段代码都是基于Python实现的。第一段代码使用了正则表达式和Python中的Counter类,实现了对文本中单词频率的统计和降序输出。第二段代码使用了正则表达式和Python中的set类,实现了对文本中以大写T开头的单词的提取和去重。
阅读全文