编程实现:编程实现英文词频统计: 文件words.txt内容如下所示: Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. 读取文件中内容,要求统计文件中英文单词的个数,输出单词及出现次数。
时间: 2024-03-16 20:47:47 浏览: 153
Python学习笔记
以下是Python代码实现:
```python
import re
# 读取文件内容
with open('words.txt', 'r') as f:
text = f.read()
# 将所有非字母字符替换为空格
text = re.sub(r'[^a-zA-Z]', ' ', text)
# 将所有单词转换为小写
text = text.lower()
# 将文本分割成单词列表
words = text.split()
# 统计单词出现次数
word_count = {}
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 输出单词及出现次数
for word, count in word_count.items():
print(f'{word}: {count}')
```
运行结果:
```
beautiful: 1
is: 4
better: 4
than: 4
ugly: 1
explicit: 1
implicit: 1
simple: 1
complex: 2
complicated: 1
```
其中,`words.txt` 文件内容为:
```
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated.
```
阅读全文