Python内置类型集合set和frozenset的使用详解


Posted in Python onApril 26, 2022

简介

集合对象 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

交集、并集、差集、对称差集

Python内置类型集合set和frozenset的使用详解

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}

无交集、子集、超集

Python内置类型集合set和frozenset的使用详解

  • 与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

运算符

Python内置类型集合set和frozenset的使用详解

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}

以上就是Python集合之set和frozenset的使用详解的详细内容!


Tags in this post...

Python 相关文章推荐
Python实现高效求解素数代码实例
Jun 30 Python
python实现用户登陆邮件通知的方法
Jul 09 Python
浅析Python中else语句块的使用技巧
Jun 16 Python
Python编写简单的HTML页面合并脚本
Jul 11 Python
Golang与python线程详解及简单实例
Apr 27 Python
Python SQLite3简介
Feb 22 Python
python 实现登录网页的操作方法
May 11 Python
Python 中的lambda函数介绍
Oct 10 Python
python编写俄罗斯方块
Mar 13 Python
聊聊python在linux下与windows下导入模块的区别说明
Mar 03 Python
用Python的绘图库(matplotlib)绘制小波能量谱
Apr 17 Python
python使用BeautifulSoup 解析HTML
Apr 24 Python
使用Python获取字典键对应值的方法
Apr 26 #Python
PyTorch中permute的使用方法
Apr 26 #Python
Python matplotlib 利用随机函数生成变化图形
方法汇总:Python 安装第三方库常用
Apr 26 #Python
Python 统计序列中元素的出现频度
Apr 26 #Python
Python matplotlib安装以及实现简单曲线的绘制
Python爬虫 简单介绍一下Xpath及使用
You might like
萌王史莱姆”萌王性别尴尬!那“萌战”归女组还是男?
2018/12/17 日漫
php反弹shell实现代码
2009/04/22 PHP
PHP乱码问题,UTF-8乱码常见问题小结
2012/04/09 PHP
php指定函数参数默认值示例代码
2013/12/04 PHP
php实现QQ空间获取当前用户的用户名并生成图片
2015/07/25 PHP
功能强大的PHP POST提交数据类
2016/07/15 PHP
实现php删除链表中重复的结点
2018/09/27 PHP
用js+xml自动生成表格的东西
2006/12/21 Javascript
jQuery查询数据返回object和字符串影响原因是什么
2013/08/09 Javascript
JavaScript中的操作符==与===介绍
2014/12/31 Javascript
浅谈jQuery页面的滚动位置scrollTop、scrollLeft
2015/05/19 Javascript
AngularJS自定义插件实现网站用户引导功能示例
2016/11/07 Javascript
js实现方块上下左右移动效果
2017/08/17 Javascript
简单的Vue SSR的示例代码
2018/01/12 Javascript
详解React开发必不可少的eslint配置
2018/02/05 Javascript
详解vue.js根据不同环境(正式、测试)打包到不同目录
2018/07/13 Javascript
vue使用keep-alive保持滚动条位置的实现方法
2019/04/09 Javascript
vue项目创建步骤及路由router
2020/01/14 Javascript
js实现滑动滑块验证登录
2020/07/24 Javascript
[04:14]从西雅图到上海——玩家自制DOTA2主题歌曲应援TI9
2019/07/11 DOTA
[57:29]Alliance vs KG 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/17 DOTA
python去掉字符串中重复字符的方法
2014/02/27 Python
python引用(import)某个模块提示没找到对应模块的解决方法
2019/01/19 Python
python lxml中etree的简单应用
2019/05/10 Python
使用python搭建服务器并实现Android端与之通信的方法
2019/06/28 Python
python修改字典键(key)的方法
2019/08/05 Python
python SocketServer源码深入解读
2019/09/17 Python
Python使用matplotlib 模块scatter方法画散点图示例
2019/09/27 Python
Python pandas RFM模型应用实例详解
2019/11/20 Python
python 实现逻辑回归
2020/12/30 Python
英国鞋类及配饰零售商:Kurt Geiger
2017/02/04 全球购物
Notino瑞典:购买香水和美容产品
2019/07/26 全球购物
乐高西班牙官方商店:LEGO Shop ES
2019/12/01 全球购物
元旦联欢会感言
2014/03/04 职场文书
坚持不是死撑,更重要的是心态
2019/08/19 职场文书
纯CSS3实现div按照顺序出入效果
2021/07/15 HTML / CSS