Python 程序:统计英语句子中单词出现次数 (无字典实现)
text = input('请输入英语句子:') # 输入英语句子
words = text.split() # 以空格为分隔符,将句子分割为单词列表
# 统计每个单词出现的次数
word_count = []
for word in words:
if word not in word_count:
word_count.append([word, 1])
else:
for i, item in enumerate(word_count):
if item[0] == word:
word_count[i][1] += 1
break
# 输出结果
for item in word_count:
print(f'{item[0]}: {item[1]}')
示例输入:
请输入英语句子: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/lIji 著作权归作者所有。请勿转载和采集!