已知水果字符串 words=”apple banana pear apple peach banana apple cherry grape cherry apple cherry lemon mango mango lemon grape apple” 编程统计每个水果单词出现的次数
时间: 2023-04-07 08:05:25 浏览: 254
我可以回答这个问题。以下是每个水果单词出现的次数:
- apple: 5
- banana: 2
- pear: 1
- peach: 1
- cherry: 3
- grape: 2
- lemon: 2
- mango: 2
相关问题
words="apple banana pear apple peach ban ana apple cherry grape cherry apple cherry I emon mangomango lemon grape apple" 编程统计每个水果单词出现的次数
若您使用 Python 编程语言,可以使用以下代码来统计每个水果单词出现的次数:
```python
words = "apple banana pear apple peach ban ana apple cherry grape cherry apple cherry I emon mangomango lemon grape apple"
# 将字符串转换为列表
word_list = words.split()
# 统计每个水果单词出现的次数
fruit_count = {}
for word in word_list:
if word in fruit_count:
fruit_count[word] += 1
else:
fruit_count[word] = 1
# 输出结果
for fruit, count in fruit_count.items():
print(fruit, ":", count)
```
输出结果为:
```
apple : 5
banana : 1
pear : 1
peach : 1
ban : 1
ana : 1
cherry : 3
grape : 2
I : 1
emon : 1
mangomango : 1
lemon : 1
```
words="apple banana pear apple peach ban ana apple cherry grape cherry apple cherry Iemon mangomango lemon grape apple" 编程统计每个水果单词出现的次数
若您使用Python语言,可以使用以下代码实现:
```python
words = "apple banana pear apple peach ban ana apple cherry grape cherry apple cherry lemon mango mango lemon grape apple"
# 将字符串转换为列表
word_list = words.split()
# 统计每个水果单词出现的次数
word_count = {}
for word in word_list:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 输出结果
for word, count in word_count.items():
print(word, count)
```
输出结果为:
```
apple 5
banana 1
pear 1
peach 1
ban 1
ana 1
cherry 3
grape 2
lemon 2
mango 2
```
阅读全文