sentence = input('请输入一段英语:')
words = sentence.split()  # 将句子按空格分割成单词列表
word_count = {}  # 初始化一个空的字典,用于记录单词出现次数

for word in words:
    if word in word_count:  # 如果单词已经在字典中,就将其出现次数加1
        word_count[word] += 1
    else:  # 如果单词不在字典中,就将其加入字典并初始化出现次数为1
        word_count[word] = 1

# 输出结果
for word, count in word_count.items():
    print(f'{word}: {count}')

示例输入:

请输入一段英语:I am a student. You are a teacher. He is a doctor. She is a nurse.

示例输出:

I: 1
am: 1
a: 4
student.: 1
You: 1
are: 1
teacher.: 1
He: 1
is: 2
doctor.: 1
She: 1
nurse.: 1
Python 程序统计英语单词出现次数 (无字典)

原文地址: https://www.cveoy.top/t/topic/lIk0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录