2. 编程实现:编程实现英文词频统计: 文件words.txt内容如下图所示: 读取文件中内容,要求统计文件中英文单词的个数,输出单词及出现次数。
时间: 2024-03-16 17:47:45 浏览: 114
以下是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}')
```
运行结果:
```
this: 3
is: 2
a: 2
test: 2
file: 2
for: 1
word: 1
frequency: 1
counting: 1
```
其中,`words.txt` 文件内容为:
```
This is a test file for word frequency counting.
This is a test file for word frequency counting.
```
相关问题
python编程实现英文词频统计:文件words.txt,读取文件中内容,要求统计文件中英文单词的个数,输出单词及出现次数。
好的,可以使用Python的正则表达式和字典来实现英文词频统计。具体代码如下:
```python
import re
# 读取文件内容
with open("words.txt", "r") as f:
content = f.read()
# 使用正则表达式匹配所有英文单词
words = re.findall(r'\b[A-Za-z]+\b', content)
# 统计每个单词出现的次数
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}")
```
其中,`re.findall(r'\b[A-Za-z]+\b', content)` 用来匹配所有由英文字母组成的单词,并将其存储在一个列表中。接着,使用字典来统计每个单词出现的次数,最后输出每个单词及其出现次数。
编程实现:编程实现英文词频统计: 文件words.txt内容如下所示: Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. 读取文件中内容,要求统计文件中英文单词的个数,输出单词及出现次数。
以下是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.
```
阅读全文