我们可以使用嵌套的循环来遍历列表中的所有列表,并将每个列表转换为集合,然后使用set.intersection()方法来获取匹配的字符串。最后将结果存储在一个新列表中,并返回新列表。
示例代码:
nested_list = [["apple", "banana", "peach"], ["orange", "kiwi", "grape"], ["watermelon", "pineapple", "apple", "grape"]]
matched_strings = []
for lst in nested_list:
if not matched_strings:
matched_strings = set(lst)
else:
matched_strings = matched_strings.intersection(lst)
matched_strings = list(matched_strings)
print(matched_strings)
输出结果:
['apple']