1 a=set([1,2,3,4,5,6]) 2 b=set([4,5,6,7]) 3 # intersection 交集 4 #a和b的交集 5 print(a.intersection(b))#{4, 5, 6} 6 print(a & b)#{4, 5, 6} 7 8 # union 并集 9 print(a.union(b))#{1, 2, 3, 4, 5, 6, 7} 10 print(a | b)#{1, 2, 3, 4, 5, 6, 7} 等价于a.union(b) 11 12 #difference 差集 13 print(a.difference(b))#{1, 2, 3} in a but not in b 14 print(a - b)#{1, 2, 3} in a but not in b 15 print(b.difference(a))#{7} 16 print(b - a)#{7} 17 print(a.symmetric_difference(b))#{1, 2, 3, 7} symmetric 对称差集 除了交集之外的所有 18 print(a ^ b)#{1, 2, 3, 7} symmetric 对称差集 除了交集之外的所有 19 20 #父集 超集 21 print(a.issuperset(b)) 22 print(a > b) 23 #子集 24 print(a.issubset(b)) 25 print(a < b)