集合的相关操作(交叉并补)
交集intersection 简写:&
set1={1,2,3,4,5,6}
set2={2,7,8,9}
res=set1&set2
print(res )
{2}
差集 difference 简写 : -
set1={1,2,3,4,5,6}
set2={2,7,8,9}
res=set1-set2
print(res )
{1, 3, 4, 5, 6}
并集 unior 简写: |
set1={1,2,3,4,5,6}
set2={2,7,8,9}
res=set1|set2
print(res )
{1, 2, 3, 4, 5, 6, 7, 8, 9}
对称差集 symmetrc_difference 简写^
set1={1,2,3,4,5,6}
set2={2,7,8,9}
res=set1^set2
print(res )
{1, 3, 4, 5, 6, 7, 8, 9}
补集
issubset 判断是否是子集 简写;小于号
set1={1,2,3,4,5,6}
set2={2,7,8,9}
res=set1<set2
print(res )
False
issuperset 判断是否是父集 简写;大于号
set1={1,2,3,4,5,6}
set2={2}
res=set1>set2
print(res )
True
isdisjoint 检测两集合是否不相交,不相交True,相交False
set1={1,2,3,4,5,