集合
集合的创建
s={5,2,3,4,2,3,4,6,3}
print(s)
"""第二种创建方式使用set()"""
s1=set(range(6))
print(s1,type(s1))
s2=set([1,2,3,4,2,2,2,7,6])
print(s2,type(s2))
s3=set((1,2,4,4,5,65))
print(s3,type(s3))
s4=set('python')
print(s4,type(s4))
s5=set({12,4,34,55,66,44,4})
print(s5,type(s5))
s6={}
print(type(s6))
s7=set()
print(type(s7))
集合的相关操作(新增删除)
s={10,20,30,40,33}
print(10 in s)
print(100 in s)
print(10 not in s)
print(100 not in s)
s.add(80)
print(s)
s.update({200,400,300})
print(s)
s.update([100,99,8])
s.update([78,64,56])
print(s)
"""集合元素的删除操作"""
s.remove(100)
print(s)
s.discard(500)
print(s)
s.pop()
print(s)
s.pop()
print(s)
s.pop()
print(s)
s.clear()
print(s)
集合间关系(判断子集,母集,交集)
"""集合间的关系"""
s={10,20,30,40}
s2={30,40,20,10}
print(s==s2)
print(s!=s2)
"""一个集合是否是另一个集合的子集"""
s1={10,20,30,40,50,60}
s2={10,20,30,40}
s3={1,2,30}
print(s2.issubset(s1))
print(s3.issubset(s1))
"""一个集合是否另一个集合的母集"""
print(s1.issuperset(s2))
print(s1.issuperset(s3))
"""两个集合是否没有交集!!!没有交集"""
print(s2.isdisjoint(s3))
s4={100,200,300}
print(s2.isdisjoint(s4))
集合的数学操作(交集、并集,差集,对称差集)
s1={10,20,30,40}
s2={20,30,40,50,60}
print(s1)
print(s2)
print(s1.intersection(s2))
print(s1&s2)
print(s1.union(s2))
print(s1|s2)
print(s1.difference(s2))
print(s1-s2)
print(s1.symmetric_difference(s2))
print(s1^s2)
集合生成式
s={i*i for i in range(12)}
print(s)
|