以下是一个使用Python编写的示例代码,用于比较三个文本列表并找到匹配的单词:
def find_matching_words(list1, list2, list3):
# 使用集合将列表中的单词转换为唯一值,以便进行比较
set1 = set(list1)
set2 = set(list2)
set3 = set(list3)
# 找到在三个集合中都存在的单词
matching_words = set1.intersection(set2, set3)
return matching_words
# 示例输入
list1 = ['apple', 'banana', 'orange', 'grape']
list2 = ['banana', 'kiwi', 'grape', 'pear']
list3 = ['grape', 'apple', 'mango', 'pineapple']
# 调用函数并打印结果
result = find_matching_words(list1, list2, list3)
print(result)
运行此代码将输出在三个列表中都存在的匹配单词(在此示例中为{'grape'}
)。请注意,此代码假设输入的列表中没有重复的单词。如果有重复的单词,集合将自动去除重复项。
上一篇:比较三个Swift枚举
下一篇:比较三个以上HashMap的键