集合
一系列不重复元素无序放在一起构成集合
存放不可变类型
声明
声明不为空的集合 s1 = {1,2,3,4}
声明空集合 s1 = set()
集合常见操作
添加
s1.add(value) 没有返回值
移除
s1.pop() 不能从空集合删除
有返回值
s1.remove(value) 删除元素必须存在
s1.discard(value) 忽略指定元素,不存在也不会报错
s1.clear() 清空集合
合并
s1.update({1,2,3,4}) 将后面的元素合并到前边 后面为可迭代类型
访问
len获取集合元素个数
in 与 not in 成员运算符
for e in s1
交集
s1 & s2
s1.intersection(s2)
并集
s1 | s2
s1.union(s2)
差集
s1 - s2
s1.difference(s2)
python字典与对象入门基础
于 2023-03-05 22:03:41 首次发布