如何找到包含集合的两个列表的区别?
List 1:
[{0, 1, 3, 9}, {2, 5, 6}, {8, 4}]
List 2:
[{6, 7, 8, 9}]
结果应为:
[{7}]
答案 0 :(得分:1)
创建两个集合并使用列表中的值填充它们。那么期望的结果就是简单的减法。
l1 = [{0, 1, 3, 9}, {2, 5, 6}, {8, 4}]
l2 = [{6, 7, 8, 9}]
s1 = set()
for i in l1:
s1.update(i)
s2 = set()
for i in l2:
s2.update(i)
print(s2 - s1)
# {7}
答案 1 :(得分:0)
听起来你可以将list1弄平,然后找出差异:
>>> l1 = [{0, 1, 3, 9}, {2, 5, 6}, {8, 4}]
>>> l2 = [{6, 7, 8, 9}]
>>> l3 = [x for y in l1 for x in y]
>>> l3
[0, 9, 3, 1, 2, 5, 6, 8, 4]
>>> l4 = [set(i for i in l2[0] if i not in l3)]
>>> l4
[set([7])]