# -*-coding:utf-8
# @Author: Glad
# @Time: 2018/8/18 18:18
a = set([11, 12, 13, 14])
b = set([13, 14, 15, 16])
# a和b的交集
# 方法一:关键字:intersection
print(a.intersection(b)) # 打印结果: {13, 14}
# 方法二:
print(a & b) # 打印结果: {13, 14}
# a并b,并集
# 方法一:关键字:union
print(a.union(b)) # 打印结果: {11, 12, 13, 14, 15, 16}
# 方法二:
print(a | b) # 打印结果: {11, 12, 13, 14, 15, 16}
# 差集
# 方法一:关键字:difference
print(a.difference(b)) # a里面有且b里面没有的 打印结果: {11, 12}
print(b.difference(a)) # b里面有且a里面没有的 打印结果: {16, 15}
# 方法二:
print(a - b) # 差集a-b ,打印结果: {11, 12}
print(b - a) # 差集b-a ,打印结果: {16, 15}
# 反向交集(对称差集)
# 方法一:关键字:symmetric_difference
print(a.symmetric_difference(b)) # 打印结果: {11, 12, 15, 16}
# 方法二:
print(a ^ b) # 打印结果: {11, 12, 15, 16}
Python的set集合(一)
最新推荐文章于 2023-11-14 17:48:16 发布