首先,我们需要将句子转换为单词列表,并将所有单词转换为小写。这可以通过使用Python的split()和lower()函数完成。
然后,我们可以创建一个空字典,并遍历单词列表。对于每个单词,我们检查它是否已在字典中存在。如果是,则将该单词添加到重复单词列表中。如果不是,则将该单词添加到字典中。
最后,我们可以输出重复单词列表,以显示句子中所有重复的单词。
以下是使用Python 3编写的示例代码:
# 输入句子
sentence = input("请输入一个句子:")
# 将句子转换为单词列表
words = sentence.lower().split()
# 创建一个空字典和一个空列表
word_dict = {}
repeat_words = []
# 遍历单词列表
for word in words:
# 检查单词是否已在字典中存在
if word in word_dict:
# 如果单词已经存在,则将其添加到重复单词列表中
if word not in repeat_words:
repeat_words.append(word)
else:
# 如果单词不存在,则将其添加到字典中
word_dict[word] = 1
# 输出重复单词列表
print("句子中的重复单词是:")
for word in repeat_words:
print(word)
示例输出:
请输入一个句子:This is a test sentence to test the program
句子中的重复单词是:
test
the