编写python程序:不用字典实现,输入段英语,仅包括单词及空格,统计其中每个单词出现的次数,并输出结果。
text = input("请输入英语句子:") # 输入英语句子
words = text.split() # 以空格为分隔符,将句子分割为单词列表
# 统计每个单词出现的次数
word_count = {}
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 输出结果
for word, count in word_count.items():
print(f"{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
原文地址: https://www.cveoy.top/t/topic/vDO 著作权归作者所有。请勿转载和采集!