str_a="python python is good and python is easy" 求字符串中每个单词出现的次数。
时间: 2024-05-14 17:15:48 浏览: 83
Python统计单词出现的次数
可以使用 Python 的字典来记录每个单词出现的次数,具体实现如下:
```python
str_a = "python python is good and python is easy"
words = str_a.split() # 将字符串拆分成单词列表
count = {} # 用字典来记录每个单词出现的次数
for word in words:
if word in count:
count[word] += 1
else:
count[word] = 1
print(count)
```
输出结果为:
```
{'python': 3, 'is': 2, 'good': 1, 'and': 1, 'easy': 1}
```
阅读全文