以下是一个比较两个二元组列表并返回匹配的二元组的示例代码:
def compare_tuples(list1, list2):
matches = []
for tuple1 in list1:
for tuple2 in list2:
if tuple1 == tuple2:
matches.append(tuple1)
return matches
# 示例用法
list1 = [(1, 2), (3, 4), (5, 6)]
list2 = [(3, 4), (7, 8), (9, 10)]
matched_tuples = compare_tuples(list1, list2)
print(matched_tuples)
输出结果为:[(3, 4)]
,因为只有(3, 4)
是同时出现在两个列表中的二元组。