Python – 元组列表交集(顺序无关)
给定元组列表,执行元素的元组交集,而不管它们的顺序。
Input : test_list1 = [(3, 4), (5, 6)], test_list2 = [(5, 4), (4, 3)]
Output : {(3, 4)}
Explanation : (3, 4) and (4, 3) are common, hence intersection ( order irrespective).
Input : test_list1 = [(3, 4), (5, 6)], test_list2 = [(5, 4), (4, 5)]
Output : set()
Explanation : No intersecting element present.
方法#1:使用 sorted() + set() + & 运算符 + 列表推导
上述功能的组合可以用来解决这个问题。在此,我们对元组进行排序,并使用 &运算符进行交集。
Python3
# Python3 code to demonstrate working of
# Tuple List intersection [ Order irrespective ]
# Using sorted() + set() + & operator + list comprehension
# initializing lists
test_list1 = [(3, 4), (5, 6), (9, 10), (4, 5)]
test_list2 = [(5, 4), (3, 4), (6, 5), (9, 11)]
# printing original list
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# Using sorted() + set() + & operator + list comprehension
# Using & operator to intersect, sorting before performing intersection
res = set([tuple(sorted(ele)) for ele in test_list1]) & set([tuple(sorted(ele)) for ele in test_list2])
# printing result
print("List after intersection : " + str(res))
Python3
# Python3 code to demonstrate working of
# Tuple List intersection [ Order irrespective ]
# Using list comprehension + map() + frozenset() + & operator
# initializing lists
test_list1 = [(3, 4), (5, 6), (9, 10), (4, 5)]
test_list2 = [(5, 4), (3, 4), (6, 5), (9, 11)]
# printing original list
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# Using list comprehension + map() + frozenset() + & operator
# frozenset used as map() requires hashable container, which
# set is not, result in frozenset format
res = set(map(frozenset, test_list1)) & set(map(frozenset, test_list2))
# printing result
print("List after intersection : " + str(res))
输出 :
The original list 1 is : [(3, 4), (5, 6), (9, 10), (4, 5)]
The original list 2 is : [(5, 4), (3, 4), (6, 5), (9, 11)]
List after intersection : {(4, 5), (3, 4), (5, 6)}
方法 #2:使用列表理解 + map() + freezeset() + &运算符
上述功能的组合可用于执行此任务。在此,我们执行将内部容器转换为集合、对其进行排序并执行交集的任务。 Frozenset 被用作它的 hashable,map() 需要 hashable 数据类型作为参数。
Python3
# Python3 code to demonstrate working of
# Tuple List intersection [ Order irrespective ]
# Using list comprehension + map() + frozenset() + & operator
# initializing lists
test_list1 = [(3, 4), (5, 6), (9, 10), (4, 5)]
test_list2 = [(5, 4), (3, 4), (6, 5), (9, 11)]
# printing original list
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# Using list comprehension + map() + frozenset() + & operator
# frozenset used as map() requires hashable container, which
# set is not, result in frozenset format
res = set(map(frozenset, test_list1)) & set(map(frozenset, test_list2))
# printing result
print("List after intersection : " + str(res))
输出 :
The original list 1 is : [(3, 4), (5, 6), (9, 10), (4, 5)]
The original list 2 is : [(5, 4), (3, 4), (6, 5), (9, 11)]
List after intersection : {frozenset({4, 5}), frozenset({5, 6}), frozenset({3, 4})}