目录
- 1. 前言
- 2. 简介
- 3. 常用操作
- 3.0 操作速览表
- 3.1 用于 set 和 frozenset 的操作
- 3.1.1 创建 set 和 frozenset -- set() frozenset()
- 3.1.2 获取集合中元素的数量 -- len(s)
- 3.1.3 检测 x 是否为 s 中的成员 -- x in s
- 3.1.4 判断两个集合是否有交集 -- isdisjoint(other)
- 3.1.5 判断集合A是集合B的子集 -- A.issubset(B)
- 3.1.6 求集合A和集合B的并集 -- A.union(B)
- 3.1.7 求集合A和集合B的交集 -- A.intersection(B)
- 3.1.8 求集合A和集合B的差集 -- A.difference(B)
- 3.1.9 求集合A和集合B的减去交集的结果 -- A.symmetric_difference(B)
- 3.1.10 对集合A进行浅拷贝 -- B = A.copy()
- 3.2 只能用于set类型的操作
- 3.2.1 集合A添加集合B所有的元素 -- A.update(B)
- 3.2.2 保留集合A和集合B中的交集 到 集合A中 -- A.intersection_update(B)
- 3.2.3 减去集合A和集合B的交集 到 集合A中 -- A.difference_update(B)
- 3.2.4 集合A和集合B的并集 减去 集合A和集合B的交集 更新的到集合A中 -- A.symmetric_difference_update(B)
- 3.2.5 为集合A添加新元素9 -- A.add(9)
- 3.2.6 从集合A中删除元素(不存在报错) -- A.remove(4)
- 3.2.7 从集合A中删除元素(不存在不报错)-- A.discard(9)
- 3.2.8 从集合A中移除并返回任意元素(集合为空报错) -- A.pop()
- 3.2.9 移除集合A中的所有元素 -- A.clear()
- 4. 参考资料
1. 前言
⭐️ 这篇主要是记录,class set([iterable])
和 class frozenset([iterable])
这两个类的构造器的使用方法
⭐️ 主要是由于在使用python3时,经常会查集合的用法,然而网上的资料比较散乱,加之也没实例,所以就自己整理一下,方便后面使用python3时,能够快速的查阅
2. 简介
⭐️ set对象是由 可哈希 对象所组成的 无序 多项集, 且多项集内的元素不重复
⭐️ 用途有:成员检测,去除重复项,数学中的集合类计算(交集,并集,差集等)
⭐️ 集合不支持索引、切片或其他序列类操作
⭐️ 内置两种集合类型:set
和 frozenset
,其中,set类型是可变的,frozenset是不可变的
⭐️ 由于set类型是可变的,他没有哈希值,因此不能被用作字典的键或其他集合元素
⭐️ frozenset类型是不可变的,且是可哈希对象,因此可以用作字典的键或其他集合的元素
⭐️ 🔗 本篇文章的源代码,提取码为 1314
3. 常用操作
3.0 操作速览表
序号 | 操作 | 操作描述 | set可用 | frozenset可用 |
---|---|---|---|---|
3.1.1 | 1️⃣ set() 2️⃣ frozenset() | 创建 set 和 frozenset对象 | ✅ | ✅ |
3.1.2 | 1️⃣ len() | 获取集合中元素的数量 | ✅ | ✅ |
3.1.3 | 1️⃣ x in s | 检测x是否为s中的成员 | ✅ | ✅ |
3.1.4 | 1️⃣ A.sdisjoint(B) | A和B是否 无交集 | ✅ | ✅ |
3.1.5 | 1️⃣ A.issubset(B) 2️⃣ A < B 3️⃣ A<=B | A是否是B的子集 | ✅ | ✅ |
3.1.6 | 1️⃣ A.union(B) 2️⃣ A | B | A和B的并集 | ✅ | ✅ |
3.1.7 | 1️⃣ A.intersection(B) 2️⃣ A & B | A和B的交集 | ✅ | ✅ |
3.1.8 | 1️⃣ A.difference(B) 2️⃣ A - B | A和B的差集 | ✅ | ✅ |
3.1.9 | 1️⃣ A.symmetric_difference(B) | A和B的并集 减去 A和B的交集 | ✅ | ✅ |
3.1.10 | 1️⃣ B = A.copy() | 对A进行浅复制 | ✅ | ✅ |
3.2.1 | 1️⃣ A.update(B) | 集合A添加集合B所有的元素 | ✅ | |
3.2.2 | 1️⃣ A.intersection_update(B) | 保留集合A和集合B中的交集 到 集合A中 | ✅ | |
3.2.3 | 1️⃣ A.difference_update(B) | 减去集合A和集合B的交集 到 集合A中 | ✅ | |
3.2.4 | 1️⃣ A.symmetric_difference_update(B) | 集合A和集合B的并集 减去 集合A和集合B的交集 更新的到集合A中 | ✅ | |
3.2.5 | 1️⃣ A.add(9) | 为集合A添加新元素9 | ✅ | |
3.2.6 | 1️⃣ A.remove(4) | 从集合A中删除元素(不存在报错)) | ✅ | |
3.2.7 | 1️⃣ A.discard(9) | 从集合A中删除元素(不存在不报错) | ✅ | |
3.2.8 | 1️⃣ A.pop() | 从集合A中移除并返回任意元素(集合为空报错) | ✅ | |
3.2.9 | 1️⃣ A.clear() | 移除集合A中的所有元素 | ✅ |
3.1 用于 set 和 frozenset 的操作
3.1.1 创建 set 和 frozenset – set() frozenset()
🔥 创建代码,记得看代码注释
# 创建空set
empty_set = set()
# 创建非空set
non_empty_set1 = {1, 2, 3}
non_empty_set2 = set([1, 2, 3, 4])
# 检查结果和类型
print(f'empty_set type: {type(empty_set)} \nconteent are {empty_set}')
print(f'non_empty_set1 type: {type(non_empty_set)} \nconteent are {non_empty_set}')
print(f'non_empty_set2 type: {type(non_empty_set2)} \nconteent are {non_empty_set2}')
# 创建空 frozenset
# 因为frozenset不可变,所以不能创建一个空集
empty_frozenset = frozenset()
# 创建非空 frozenset
non_empty_frozenset1 = frozenset([1, 2, 3, 4])
non_empty_frozenset2 = frozenset(non_empty_set1)
# 检查结果和类型
print(f'empty_frozenset type: {type(empty_frozenset)} \nconteent are {empty_frozenset}')
print(f'non_empty_frozenset1 type: {type(non_empty_frozenset1)} \nconteent are {non_empty_frozenset1}')
print(f'non_empty_frozenset2 type: {type(non_empty_frozenset2)} \nconteent are {non_empty_frozenset2}')
🔥 运行结果
empty_set type: <class 'set'>
conteent are set()
non_empty_set1 type: <class 'set'>
conteent are {1, 2, 3}
non_empty_set2 type: <class 'set'>
conteent are {1, 2, 3, 4}
empty_frozenset type: <class 'frozenset'>
conteent are frozenset()
non_empty_frozenset1 type: <class 'frozenset'>
conteent are frozenset({1, 2, 3, 4})
non_empty_frozenset2 type: <class 'frozenset'>
conteent are frozenset({1, 2, 3})
3.1.2 获取集合中元素的数量 – len(s)
🔥 代码
### 3.1.2 获取集合中元素的数量 -- len(s)
print(f'non_empty_set1 为: {non_empty_set1}\n')
print(f"non_empty_set1 length: {len(non_empty_set1)}" )
🔥 结果
non_empty_set1 为: {1, 2, 3}
non_empty_set1 length: 3
3.1.3 检测 x 是否为 s 中的成员 – x in s
🔥 代码
### 3.1.3 检测 x 是否为 s 中的成员 -- x in s
print(f'non_empty_set1 为: {non_empty_set1}')
print(f'non_empty_set2 为: {non_empty_set2}\n')
print(f"1 在 non_empty_set2 中? {1 in non_empty_set2}" )
print(f"10 在 non_empty_set2 中? {10 in non_empty_set2}" )
print(f"non_empty_set1 在 non_empty_set2 中? {non_empty_set1 in non_empty_set2} \n")
🔥 结果
non_empty_set1 为: {1, 2, 3}
non_empty_set2 为: {1, 2, 3, 4}
1 在 non_empty_set2 中? True
10 在 non_empty_set2 中? False
non_empty_set1 在 non_empty_set2 中? False
3.1.4 判断两个集合是否有交集 – isdisjoint(other)
🔥 代码
### 3.1.4 判断两个集合是否有交集 -- isdisjoint(other)
non_empty_set3 = {5, 6, 7}
non_empty_set4 = {4, 5, 6, 7}
print(f'non_empty_set2 为: {non_empty_set2}')
print(f'non_empty_set3 为: {non_empty_set3}')
print(f'non_empty_set4 为: {non_empty_set4}\n')
print(f"non_empty_set3 和 non_empty_set2 无交集? { non_empty_set2.isdisjoint(non_empty_set3)}" )
print(f"non_empty_set4 和 non_empty_set2 无交集? { non_empty_set2.isdisjoint(non_empty_set4)}" )
🔥 结果
non_empty_set2 为: {1, 2, 3, 4}
non_empty_set3 为: {5, 6, 7}
non_empty_set4 为: {4, 5, 6, 7}
non_empty_set3 和 non_empty_set2 无交集? True
non_empty_set4 和 non_empty_set2 无交集? False
3.1.5 判断集合A是集合B的子集 – A.issubset(B)
🔥 代码
### 3.1.5 判断集合A是集合B的子集 -- A.issubset(B)
print(f'non_empty_set1 为: {non_empty_set1}')
print(f'non_empty_set2 为: {non_empty_set2}\n')
print(f"non_empty_set1 是 non_empty_set2 的子集? { non_empty_set1.issubset(non_empty_set2)}" )
print(f"non_empty_set1 是 non_empty_set2 的子集? { non_empty_set1 <= non_empty_set2}" )
print(f"non_empty_set1 是 non_empty_set2 的真子集? { non_empty_set1 < non_empty_set2}" )
🔥 结果
non_empty_set1 为: {1, 2, 3}
non_empty_set2 为: {1, 2, 3, 4}
non_empty_set1 是 non_empty_set2 的子集? True
non_empty_set1 是 non_empty_set2 的子集? True
non_empty_set1 是 non_empty_set2 的真子集? True
3.1.6 求集合A和集合B的并集 – A.union(B)
🔥 代码
### 3.1.6 求集合A和集合B的并集 -- A.union(B)
print(f'non_empty_set2 为: {non_empty_set2}')
print(f'non_empty_set4 为: {non_empty_set4}\n')
print(f"non_empty_set4 和 non_empty_set2 的并集是? { non_empty_set4.union(non_empty_set2)}" )
print(f"non_empty_set4 和 non_empty_set2 的并集是? { non_empty_set4 | non_empty_set2}" )
🔥 结果
non_empty_set2 为: {1, 2, 3, 4}
non_empty_set4 为: {4, 5, 6, 7}
non_empty_set4 和 non_empty_set2 的并集是? {1, 2, 3, 4, 5, 6, 7}
non_empty_set4 和 non_empty_set2 的并集是? {1, 2, 3, 4, 5, 6, 7}
3.1.7 求集合A和集合B的交集 – A.intersection(B)
🔥 代码
### 3.1.7 求集合A和集合B的交集 -- A.intersection(B)
print(f'non_empty_set2 为: {non_empty_set2}')
print(f'non_empty_set4 为: {non_empty_set4}\n')
print(f"non_empty_set4 和 non_empty_set2 的交集是? { non_empty_set4.intersection(non_empty_set2)}" )
print(f"non_empty_set4 和 non_empty_set2 的交集是? { non_empty_set4 & non_empty_set2}" )
🔥 结果
non_empty_set2 为: {1, 2, 3, 4}
non_empty_set4 为: {4, 5, 6, 7}
non_empty_set4 和 non_empty_set2 的交集是? {4}
non_empty_set4 和 non_empty_set2 的交集是? {4}
3.1.8 求集合A和集合B的差集 – A.difference(B)
🔥 代码
print(f'non_empty_set2 为: {non_empty_set2}')
print(f'non_empty_set4 为: {non_empty_set4}\n')
print(f"non_empty_set4 对 non_empty_set2 的差集是? { non_empty_set4.difference(non_empty_set2)}" )
print(f"non_empty_set4 对 non_empty_set2 的差集是? { non_empty_set4 - non_empty_set2}" )
🔥 结果
non_empty_set2 为: {1, 2, 3, 4}
non_empty_set4 为: {4, 5, 6, 7}
non_empty_set4 对 non_empty_set2 的差集是? {5, 6, 7}
non_empty_set4 对 non_empty_set2 的差集是? {5, 6, 7}
3.1.9 求集合A和集合B的减去交集的结果 – A.symmetric_difference(B)
🔥 代码
### 3.1.9 求集合A和集合B的减去交集的结果 -- A.symmetric_difference(B)
print(f'non_empty_set2 为: {non_empty_set2}')
print(f'non_empty_set4 为: {non_empty_set4}\n')
print(f"non_empty_set4 和 non_empty_set2 减去交集是? { non_empty_set4.symmetric_difference(non_empty_set2)}" )
🔥 结果
non_empty_set2 为: {1, 2, 3, 4}
non_empty_set4 为: {4, 5, 6, 7}
non_empty_set4 和 non_empty_set2 减去交集是? {1, 2, 3, 5, 6, 7}
3.1.10 对集合A进行浅拷贝 – B = A.copy()
🔥 浅拷贝就是只拷贝父对象,不拷贝子对象,具体可查看这篇文章:Python 直接赋值、浅拷贝和深度拷贝解析
🔥 代码
### 3.1.0 对集合A进行浅拷贝 -- B = A.copy()
print(f'non_empty_set2 为: {non_empty_set2} \n')
print(f"non_empty_set2 的浅复制为: {non_empty_set2.copy()}" )
🔥 结果
non_empty_set2 为: {1, 2, 3, 4}
non_empty_set2 的浅复制为: {1, 2, 3, 4
3.2 只能用于set类型的操作
3.2.1 集合A添加集合B所有的元素 – A.update(B)
🔥 代码
### 3.2.1 集合A添加集合B所有的元素 -- A.update(B)
A = non_empty_set4.copy()
B = non_empty_set2.copy()
print(f"集合 A 为: {A}" )
print(f"集合 B 为: {B} \n" )
A.update(B)
print(f"为集合A 添加集合B中的元素后为: {A}" )
🔥 结果
集合 A 为: {4, 5, 6, 7}
集合 B 为: {1, 2, 3, 4}
为集合A 添加集合B中的元素后为: {1, 2, 3, 4, 5, 6, 7}
3.2.2 保留集合A和集合B中的交集 到 集合A中 – A.intersection_update(B)
🔥 代码
### 3.2.2 保留集合A和集合B中的交集 到 集合A中 -- A.intersection_update(B)
A = non_empty_set4.copy()
B = non_empty_set2.copy()
print(f"集合 A 为: {A}" )
print(f"集合 B 为: {B} \n" )
A.intersection_update(B)
print(f"更新后的集合A为: {A}" )
🔥 结果
集合 A 为: {4, 5, 6, 7}
集合 B 为: {1, 2, 3, 4}
更新后的集合A为: {4}
3.2.3 减去集合A和集合B的交集 到 集合A中 – A.difference_update(B)
🔥 代码
### 3.2.3 减去集合A和集合B的交集 到 集合A中 -- A.difference_update(B)
A = non_empty_set4.copy()
B = non_empty_set2.copy()
print(f"集合 A 为: {A}" )
print(f"集合 B 为: {B} \n" )
A.difference_update(B)
print(f"更新后的集合A为: {A}" )
🔥 结果
集合 A 为: {4, 5, 6, 7}
集合 B 为: {1, 2, 3, 4}
更新后的集合A为: {5, 6, 7}
3.2.4 集合A和集合B的并集 减去 集合A和集合B的交集 更新的到集合A中 – A.symmetric_difference_update(B)
🔥 代码
### 3.2.4 集合A和集合B的并集 减去 集合A和集合B的交集 更新的到集合A中 -- A.symmetric_difference_update(B)
A = non_empty_set4.copy()
B = non_empty_set2.copy()
print(f"集合 A 为: {A}" )
print(f"集合 B 为: {B} \n" )
A.symmetric_difference_update(B)
print(f"更新后的集合A为: {A}" )
🔥 结果
集合 A 为: {4, 5, 6, 7}
集合 B 为: {1, 2, 3, 4}
更新后的集合A为: {1, 2, 3, 5, 6, 7}
3.2.5 为集合A添加新元素9 – A.add(9)
🔥 代码
### 3.2.5 为集合A添加新元素9 -- A.add(9)A = non_empty_set4.copy()
A = non_empty_set4.copy()
print(f"集合 A 为: {A}\n" )
A.add(9)
print(f"更新后的集合A为: {A}" )
🔥 结果
集合 A 为: {4, 5, 6, 7}
更新后的集合A为: {4, 5, 6, 7, 9}
3.2.6 从集合A中删除元素(不存在报错) – A.remove(4)
🔥 代码
### 3.2.6 从集合A中删除元素 -- A.remove(4)
A = non_empty_set4.copy()
print(f"集合 A 为: {A}\n" )
A.remove(4)
print(f"更新后的集合A为: {A}" )
🔥 结果
集合 A 为: {4, 5, 6, 7}
更新后的集合A为: {5, 6, 7}
3.2.7 从集合A中删除元素(不存在不报错)-- A.discard(9)
🔥 代码
### 3.2.7 从集合A中删除元素(不存在不报错)-- A.discard(9)
A = non_empty_set4.copy()
print(f"集合 A 为: {A}\n" )
A.discard(9)
print(f"更新后的集合A为: {A}" )
🔥 结果
集合 A 为: {4, 5, 6, 7}
更新后的集合A为: {4, 5, 6, 7}
3.2.8 从集合A中移除并返回任意元素(集合为空报错) – A.pop()
🔥 代码
### 3.2.8 从集合A中移除并返回任意元素 -- A.pop()
A = non_empty_set4.copy()
print(f"集合 A 为: {A}\n" )
rv = A.pop()
print(f"更新后的集合A为: {A} 返回值为: {rv}" )
🔥 结果
集合 A 为: {4, 5, 6, 7}
更新后的集合A为: {5, 6, 7} 返回值为: 4
3.2.9 移除集合A中的所有元素 – A.clear()
🔥 代码
### 3.2.9 移除集合A中的所有元素 -- A.clear()
A = non_empty_set4.copy()
print(f"集合 A 为: {A}\n" )
rv = A.clear()
print(f"更新后的集合A为: {A}" )
🔥 结果
集合 A 为: {4, 5, 6, 7}
更新后的集合A为: set()