回顾完基础知识之后,我们看一下今天的重点内容,那就是集合的交集、并集和差集:
我们先定义两个集合:
In [6]: set1 = {1,2,3,4,5}
In [7]: set2 = {3,4,5,6,7...}
然后我们进行相关操作:
In [8]: set1 & set2 # 交集
Out[8]: {3, 4, 5}
In [9]: set1 | set2 # 并集
Out[9]: {1, 2..., 3, 4, 5, 6, 7}
In [10]: set1 - set2 # 差集
Out[10]: {1, 2}
In [11]: set2 - set1 # 差集
Out[11]: {6, 7}