python 交集 并集 实例

q1=[1,2,3]
q2=[2,3,4]
a = set(q1)
b = set(q2)
c = a ^ b
d = a & b # 交集
e = a | b # 并集
print(c)
print(d)
print(e)
print(a.intersection(c)) # 交集
print(b.intersection(c)) # 交集

print结果:
{1, 4}
{2, 3}
{1, 2, 3, 4}
{1}
{4}

猜你喜欢

转载自blog.csdn.net/guotong1988/article/details/80774979