set 集合 不重复,无序,内容必须可hash (不可变)
可用来去重复
lst = [1,2,3,4,1,2,3,4,5]
s = set(lst)
lst = list(s)
print(lst)
[1,2,3,4,5]
增加
.add
.update 迭代添加
.update(“abc”) {“a”, “b”, “c”}
删除
.remove("") 直接删除,不存在会报错
.pop() 随机删除 无法选择性删除
.clear() 清空
改
先用
.remove("")删除
再用
.add("")添加
查
set是可迭代对象,所以可以用for循环
for el in s:
print(el)
常用操作
交集
print(s1&s2)
print(s1.intersection(s2))
并集
(s1 | s2)
s1.union(s2)
差集
(s1 - s2)
s1.difference(s2)
反交集
(s1 ^ s2)
(s1.symmetric_difference(s2))
子集
s1<s2
s1.issubset(s2))
超集
s1>s2
s1.issuperset
frozenset变可哈希 冰冻
深浅拷贝
赋值:没有创建新对象,两个变量同时指向一个对象
浅拷贝,拷贝第一层内容
lst2 = lst1[:] 浅拷贝 切片
lst2 = lst1.copy() 浅拷贝
深拷贝
拷贝所有和当前对象相关联的内容
引入copy模块
import copy
lst2 = copy.deepcopy(lst1)