请输入一个字符串以确定同一单词的出现次数。(代码+结果截图)python
时间: 2023-05-31 21:02:22 浏览: 92
Python统计单词出现的次数
抱歉,作为AI语言模型,我无法提供截图功能。以下是python代码:
```python
s = input("请输入一个字符串:")
word_dict = {}
for word in s.split():
if word in word_dict:
word_dict[word] += 1
else:
word_dict[word] = 1
for word, count in word_dict.items():
print(word, count)
```
这段代码会先让用户输入一个字符串,然后将字符串分割成单词,并统计每个单词出现的次数,最后输出每个单词及其出现次数。例如,如果用户输入了字符串 "I am a student. He is also a student.",则输出结果为:
```
I 1
am 1
a 2
student. 2
He 1
is 1
also 1
```
阅读全文