# 2 issuperset 等同于 >=
print(a.issuperset(b)) # 测试 b 的元素是否都在 a 中
print(a>=b)
>>> {1, 2, 3, 4, 5, 6, 7, 8, 9}
False
False
# 3 union 和 | 是并集的意思
print(a|b)
print(a.union(b))
>>> {1, 2, 3, 4, 5, 6, 7, 8, 9}
{1, 2, 3, 4, 5, 6, 7, 8, 9}
# 并集之后,set会对元素进行去重
# 4 intersection 和 & 是交集的意思
print(a.intersection(b))
print(a&b)
>>> {4, 5, 6}
{4, 5, 6}
# 交集就是两个set的都有的元素
# 5 difference 和 —
print(a.difference(b))
print(a-b)
>>> {1, 2, 3}
{1, 2, 3}
# 返回 a 中有,而 b 中没有的元素
# 6 symmetric_difference 和 ^
print(a.symmetric_difference(b))
print(a^b)
>>> {1, 2, 3, 7, 8, 9}
{1, 2, 3, 7, 8, 9}
# 返回 a 和 b 中不重复的元素
# 7 特别需要注意:
a={1,2,3,4,5,6}
b={4,5,6,7,8,9}
c=a|b
print(type(c))
print(c)
>>>
{1, 2, 3, 4, 5, 6, 7, 8, 9}
# 所有集合的运算结果返回的都是 set 类型。
# 8 set 的浅复制
d=a.copy()
print(d)
>>> {1,2,3,4,5,6}
# 9 set的深复制,暂且不谈,以后会提到