Python集合之set和frozenset的使用详解
作者:XerCis 发布时间:2021-06-26 22:41:20
标签:Python,集合,set,frozenset
简介
集合对象 set 是由具有唯一性的可哈希对象组成的无序多项集,如 list 不能哈希因此,不能作为 set 的一项。
set 的常见用途包括成员检测、从序列中去除重复项以及数学中的集合类计算,如交集、并集、差集与对称差集等。
set 不记录元素位置或插入顺序。 相应地,set 不支持索引、切片或其他序列操作。
目前有两种内置集合类型,set 和 frozenset:
set 是可变的,其内容可以通过 add() 和 remove() 来改变。由于是可变类型,所以不可哈希值,则不能作为字典的键或其他集合的元素。
frozenset 是不可变的,所以可哈希,因此可以用为字典的键或其他集合的元素。
除了可以使用 set 构造器,非空的 set (不是 frozenset) 还可以通过将以逗号分隔的元素列表包含于花括号之内来创建,例如: {‘jack’, ‘sjoerd’}。
构造
花括号内用逗号分隔
集合推导式
类型构造器
a = {1, 2, 3} # 花括号内用逗号分隔
b = {i for i in range(4) if i > 0} # 集合推导式
c = set([1, 2, 3]) # 类型构造器
print(a, type(a))
print(b, type(b))
print(c, type(c))
# {1, 2, 3} <class 'set'>
# {1, 2, 3} <class 'set'>
# {1, 2, 3} <class 'set'>
基本使用
a = {1, 2, 3}
print(len(a)) # 元素数量
print(1 in a) # 检测成员
print(1 not in a) # 检测成员
# 3
# True
# False
交集、并集、差集、对称差集
A = {1, 2, 3}
B = {3, 4, 5}
print(A.intersection(B)) # 交集
print(A.union(B)) # 并集
print(A.difference(B)) # 差集
print(A.symmetric_difference(B)) # 对称差集
# {3}
# {1, 2, 3, 4, 5}
# {1, 2}
# {1, 2, 4, 5}
无交集、子集、超集
与other无交集:
isdisjoint(other)
是other的子集:
issubset(other)
,相当于set <= other
是other的超集:
issuperset(other)
,相当于set >= other
A = {1, 2, 3}
B = {3, 4, 5}
C = {1, 2, 3, 4, 5, 6}
D = {7, 8, 9}
# 是否无交集
print(A.isdisjoint(A)) # False
print(A.isdisjoint(B)) # False
print(A.isdisjoint(C)) # False
print(A.isdisjoint(D)) # True
print()
# 是否子集
print(A.issubset(A)) # True
print(A.issubset(B)) # False
print(A.issubset(C)) # True
print(A.issubset(D)) # False
print()
# 是否超集
print(C.issuperset(A)) # True
print(C.issuperset(B)) # True
print(C.issuperset(C)) # True
print(C.issuperset(D)) # False
运算符
A、B、C 是 C 的子集,C 是 A、B、C 的超集
A、B 是 C 的真子集,C 是 A、B 的真超集
运算符 | 含义 |
---|---|
<= | 子集 |
< | 真子集 |
>= | 超集 |
> | 真超集 |
& | 交集 |
| | 并集 |
- | 差集 |
^ | 对称差集 |
A = {1, 2, 3}
B = {3, 4, 5}
C = {1, 2, 3, 4, 5, 6}
D = {7, 8, 9}
# 子集,相当于issubset(other)
print(A <= C) # True
print(B <= C) # True
print(C <= C) # True
print(D <= C) # False
print()
# 真子集
print(A < C) # True
print(B < C) # True
print(C < C) # False
print(D < C) # False
print()
# 超集,相当于issuperset(other)
print(C >= A) # True
print(C >= B) # True
print(C >= C) # True
print(C >= D) # False
print()
# 真超集
print(C > A) # True
print(C > B) # True
print(C > C) # False
print(C > D) # False
print()
# 交集,相当于intersection(*other)
print(A & B) # {3}
print(A.intersection(B)) # {3}
# 并集,相当于union(*other)
print(A | B) # {1, 2, 3, 4, 5}
print(A.union(B)) # {1, 2, 3, 4, 5}
# 差集,相当于difference(*other)
print(A - B) # {1, 2}
print(A.difference(B)) # {1, 2}
# 对称差集,相当于symmetric_difference(other)
print(A ^ B) # {1, 2, 4, 5}
print(A.symmetric_difference(B)) # {1, 2, 4, 5}
可用于 set 的操作
不可用于不可变的 frozenset
操作 | 含义 |
---|---|
add(elem) | 添加单个元素 |
remove(elem) | 移除单个元素,如果不存在报错 |
discard(elem) | 移除单个元素,如果存在 |
pop() | 移除并返回任一元素,集合为空报错 |
clear() | 移除所有元素 |
update(*others) set |= other | 添加元素 |
difference_update(*others) set -= other | 移除元素 |
symmetric_difference_update(other) set ^= other | 移除相同元素 |
A = {1, 2, 3}
A.add(4) # 添加单个元素
print(A) # {1, 2, 3, 4}
A = {1, 2, 3}
A.remove(3) # 移除单个元素,如果不存在报错
print(A) # {1, 2}
A = {1, 2, 3}
A.discard(3) # 移除单个元素,如果存在
A.discard(99) # 移除单个元素,如果存在
print(A) # {1, 2}
A = {2, 1, 3}
print(A.pop()) # 移除并返回任一元素,集合为空报错
A = {1, 2, 3}
A.clear() # 移除所有元素
print(A) # set()
A = {1, 2, 3}
B = {3, 4, 5}
A.update(B) # 添加元素
# A |= B # 添加元素
print(A) # {1, 2, 3, 4, 5}
A = {1, 2, 3}
B = {3, 4, 5}
A.difference_update(B) # 移除元素
# A -= B # 移除元素
print(A) # {1, 2}
A = {1, 2, 3}
B = {3, 4, 5}
A.symmetric_difference_update(B) # 移除相同元素
# A ^= B # 移除相同元素
print(A) # {1, 2, 4, 5}
来源:https://xercis.blog.csdn.net/article/details/118487355
0
投稿
猜你喜欢
- 1. 检查HTML元素是否有拼写错误、是否忘记结束标记 即使是老手也经常会弄错div的嵌套关系。可以用dreamweaver的验证功能检查一
- 前言这一期我们继续完善我们的魔塔小游戏。废话不多说,让我们愉快地开始吧~开发工具Python版本: 3.7.4相关模块:cpgam
- 查询效率分析:子查询为确保消除重复值,必须为外部查询的每个结果都处理嵌套查询。在这种情况下可以考虑用联接查询来取代。如果要用子查询,那就用E
- <%'asp事务处理。'测试数据库为sql server,服务器为本机,数据库名为test,表名为a,两个字段id(i
- UPA2008于2008年10月24日在深圳举行,托哥、绿桔应邀主持了一场圆桌会和一场工作坊,以下是圆桌会议《商业价值与用户价值的平衡》的现
- Oracle关系型数据库管理系统是世界上流行的关系数据库,它是一个极其强大、灵活和复杂的系统,据说,在使用oracle时应有这样的思想,那就
- 按照本文操作和体会,会对sql优化有个基本最简单的了解,其他深入还需要更多资料和实践的学习: 1. 建表: 代码如下:creat
- 最近在学习python,做一些python练习题github上几年前的练习题有一题是这样的:使用 Python 实现:对着电脑吼一声,自动打
- 程序如下:<%Function GetEmploymentStatusListDim dd = Ap
- 实例如下:/** * 数字格式转换成千分位 *@param{Object}num */function commafy(num){ &nbs
- 一、常用按键按键说明Keys.BACK_SPACE回退键(BackSpace)Keys.TAB制表键(Tab)Keys.ENTER回车键(E
- 在数据库查询的时候,我们有时有这样的需求,就是要找出数据表里指定范围行内的数据记录,比如说要找出数据表里第10行到第20行的这10条数据,那
- 有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的。那么我们使用Python如何调用Linux的
- 看看下面:<%Set objQuery = Server.CreateObject("ixss
- 随着十几年前“用户体验”这一概念的提出,“用户研究”也逐渐发展成为一个新兴的行业。那么,“用户研究”究竟包括哪些工作内容,在企业中如何开展,
- 常见的误解有: 1. 只用 ado.net ,无法进行动态 SQL 拼接。 2. 有几个动态参数,代码的重复量就成了这些参数的不同数量的组合
- 背景 还是学院和专业的关系,我需要保持点击提交按钮后,页面select中继续维持提交前的值 网上有几种办法,要么通过url跳转时候附带参数形
- 最近对微格式进行了一些学习,在学习过程中收获不少。在此分享下,欢迎交流!微型格式的优点:1,语义化的HTML和CSS类名称来标记共同内容。2
- 利用空闲几天把《JavaScript权威指南》安静的读了一篇。真是一本好书呀!呵呵,这句话见的太多了。好在什么地方呢?听我慢慢道来。从开始接
- 如何在庞大的数据中高效的检索自己需要的东西?本篇内容介绍了Python做出一个大数据搜索引擎的原理和方法,以及中间进行数据分析的原理也给大家