分别输入若干个单词,每个单词一行:单词有小写字母组成,统计每个单词出现的次数,并输出输入: • about• send• about•me。输出:•2 about•1 me•1 send
时间: 2023-05-31 19:08:02 浏览: 44
统计单词出现次数是一道常见的编程问题,这里提供一个简单的Python代码实现:
word_dict = {}
while True:
word = input()
if not word:
break
if word in word_dict:
word_dict[word] += 1
else:
word_dict[word] = 1
for word, count in word_dict.items():
print(word, count)
针对您的问题,输入应该是:
about
send
about
me
输出结果应该是:
about 2
send 1
me 1
相关问题
分别输入若干个单词,每个一行,单词有小写字母组成,统计每个单词出现的次数,并输出。
好的,这是一个编程问题,我可以为您提供代码参考。以下是 Python 代码示例:
```python
word_counts = {}
while True:
word = input("请输入单词(输入空行结束):")
if word == "":
break
if word not in word_counts:
word_counts[word] = 1
else:
word_counts[word] += 1
print("每个单词出现的次数为:")
for word, count in word_counts.items():
print(word + ":" + str(count))
```
请将您的单词依次输入,每个单词一行,输入空行以结束。程序会统计每个单词出现的次数,并输出结果。如果您还有其他问题,欢迎继续提问。
分别输入若干个单词,每个一行,单词由小写字母组成,统计每个单词出现的次数,并输出。
可以使用一个字典来记录每个单词出现的次数。具体的步骤如下:
1. 创建一个空字典 word_count,用于记录每个单词出现的次数。
2. 循环输入若干个单词,对于每个单词执行以下操作:
a. 如果该单词已经在 word_count 中出现过,将该单词出现的次数加 1。
b. 如果该单词没有在 word_count 中出现过,将该单词添加到 word_count 中,出现次数初始化为 1。
3. 循环遍历 word_count,输出每个单词和其出现的次数。
具体实现代码如下:
```
word_count = {}
# 输入单词
while True:
word = input()
if not word:
break
# 统计单词出现次数
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 输出结果
for word, count in word_count.items():
print(word, count)
```
注意,我们使用了一个无限循环,当输入空行时跳出循环。这是因为我们不知道要输入多少个单词,只有当用户输入空行时,我们才知道输入结束了。