set集合

不同元素组成

无序

集合中元素必须是不可变的类型

s={1,2,3,4,5,6}#增加s.add("s")#清空s.clear()#弹出s.pop()#删除指定值,删除元素不存在报错s.remove("s")#删除指定值,删除元素不存在不报错s.discard("ss")li=["xi","ha","list"]lx_li = [ 'hua','ha']new_li = set(li)new_lx=set(lx_li)print(new_li,new_lx)#求交集print(new_li.intersection(new_lx))print(new_li&new_lx)#求并集print(new_li.union(new_lx))print(new_li|new_lx)#差集【方向】print(new_li-new_lx)print(new_li.difference(new_lx))print(new_lx-new_li)#差集difference_updatenew_li=new_li-new_lxprint(new_li)new_li.difference_update(new_lx)s1={1,2}s2={1,2,3}print(s1.issubset(s2)) #si 是s2的子集print(s2.issubset(s1)) #Falseprint(s2.issuperset(s1)) #s2是s1的父集#update更新多个值#将s2覆盖到s1s1.update(s2)#传递元祖s1.update((5,6))#传递lists1.update([7,8])#s1.add(7,4)更新一个值# frozenset不可变集合s=frozenset("hello")