可以使用IN关键字来比较两个元组列表。下面是一个示例代码:
tuple_list1 = [(1, 2), (3, 4), (5, 6)]
tuple_list2 = [(3, 4), (7, 8), (9, 10)]
for tuple1 in tuple_list1:
if tuple1 in tuple_list2:
print(tuple1, "is in tuple_list2")
else:
print(tuple1, "is not in tuple_list2")
上述代码中,我们首先定义了两个元组列表tuple_list1
和tuple_list2
。然后,我们使用一个循环来遍历tuple_list1
中的每个元组。在每次迭代中,我们使用IN关键字来判断当前元组是否存在于tuple_list2
中。如果存在,我们打印出该元组存在于tuple_list2
中的信息;如果不存在,我们打印出该元组不存在于tuple_list2
中的信息。
运行上述代码,输出如下:
(1, 2) is not in tuple_list2
(3, 4) is in tuple_list2
(5, 6) is not in tuple_list2
可以看到,第一个元组(1, 2)
不存在于tuple_list2
中,第二个元组(3, 4)
存在于tuple_list2
中,第三个元组(5, 6)
也不存在于tuple_list2
中。
上一篇:比较两个元组列表并查找不同的值