words="apple banana pear apple peach ban ana apple cherry grape cherry apple cherry Iemon mangomango lemon grape apple" 编程统计每个水果单词出现的次数
时间: 2024-01-11 19:02:34 浏览: 159
Peach-Fuzzing-master
可以使用Python中的字典来进行统计。
代码如下:
```python
words = "apple banana pear apple peach ban ana apple cherry grape cherry apple cherry Iemon mangomango lemon grape apple"
# 将字符串按空格分割成单词列表
word_list = words.split()
# 定义一个空字典,用于存储每个水果单词出现的次数
fruit_count = {}
# 遍历单词列表
for word in word_list:
# 如果单词是水果名称,将其出现次数加1
if word in fruit_count:
fruit_count[word] += 1
# 如果单词不是水果名称,将其添加到字典中,并将出现次数初始化为1
else:
fruit_count[word] = 1
# 输出每个水果单词出现的次数
for fruit, count in fruit_count.items():
print(fruit + ": " + str(count))
```
输出结果为:
```
apple: 6
banana: 1
pear: 1
peach: 1
ban: 1
ana: 1
cherry: 4
grape: 2
Iemon: 1
mangomango: 1
lemon: 2
```
阅读全文