#update
set1= {'alex','wusir','ritian','egon','barry'}
set1.update('abc')
print(set1)
删
1,pop
set.pop()#随机删除,有返回值
2,remove
remove#按元素去删。没有这个元素会报错
3,clear
clear#清空集合
4,del
del 删除集合。---》会报错
查
只能用for去查
3、集合的交集并集差集
1,交集:
set1 ={1,2,3,4,5}
set2 ={4,5,6,7,8}
set3= set1&set2print(set)
或者
set1 ={1,2,3,4,5}
set2={4,5,6,7,8}print(set1.intersection(set2))
2,并集
set1 ={1,2,3,4,5}
set2={4,5,6,7,8}print(set1 | set2)
或者
set1 ={1,2,3,4,5}
set2={4,5,6,7,8}print(set2.union(set1))
3,反交集
set1 ={1,2,3,4,5}
set2={4,5,6,7,8}print(set1 ^ set2)
或者
set1 ={1,2,3,4,5}
set2={4,5,6,7,8}print(set1.symmetric_difference set2)
4,差集
set1 ={1,2,3,4,5}
set2={4,5,6,7,8}print(set1.difference(set2))
或者:
set1 ={1,2,3,4,5}
set2={4,5,6,7,8}print(set1-set2)
面试题:(去重)
li=[1,2,33,33,2,1,4,5,6,6]
#去除重复元素----set
s1 = set(li)
print(set1) #{1,2,33,4,5,6}
li = list(set1)
print(li)
---------------------------------------------------------------------------------------------------------------------------------------
4、将集合转化为不可变数据类型
s =frozenset('barry')---->frozen类型
5,深浅拷贝
1,浅copy
li =[1,2,3]
li2 = li.copy()
copy产生了一个新的列表----》copy----》浅copy
2,深copy
浅copy。赋值运算是共用一个ip地址,切片再赋值是浅copy
补充:
枚举
for i in enumerate(li):
print(i)