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文件写入实例分析
Apr 08 Python
Python基于scrapy采集数据时使用代理服务器的方法
Apr 16 Python
python的keyword模块用法实例分析
Jun 30 Python
一波神奇的Python语句、函数与方法的使用技巧总结
Dec 08 Python
使用PyInstaller将Python程序文件转换为可执行程序文件
Jul 08 Python
Django项目中model的数据处理以及页面交互方法
May 30 Python
Ubuntu下Python2与Python3的共存问题
Oct 31 Python
python爬取cnvd漏洞库信息的实例
Feb 14 Python
Python实现计算文件MD5和SHA1的方法示例
Jun 11 Python
Python namedtuple命名元组实现过程解析
Jan 08 Python
python matplotlib画盒图、子图解决坐标轴标签重叠的问题
Jan 19 Python
Python Pandas读取Excel日期数据的异常处理方法
Feb 28 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
利用PHP创建动态图像
2006/10/09 PHP
php生成SessionID和图片校验码的思路和实现代码
2009/03/10 PHP
真正根据utf8编码的规律来进行截取字符串的函数(utf8版sub_str )
2012/10/24 PHP
php url路由入门实例
2014/04/23 PHP
PHP获取文件夹大小函数用法实例
2015/07/01 PHP
laravel框架中控制器的创建和使用方法分析
2019/11/23 PHP
快速保存网页中所有图片的方法
2006/06/23 Javascript
JSON扫盲帖 JSON.as类教程
2009/02/16 Javascript
JavaScript 常见安全漏洞和自动化检测技术
2015/08/21 Javascript
javascript设计简单的秒表计时器
2020/09/05 Javascript
jQuery控制frames及frame页面JS的方法
2016/03/08 Javascript
js 获取站点应用名的简单实例
2016/08/18 Javascript
jQuery EasyUI Accordion可伸缩面板组件使用详解
2017/02/28 Javascript
详解angularJS动态生成的页面中ng-click无效解决办法
2017/06/19 Javascript
静态页面实现 include 引入公用代码的示例
2017/09/25 Javascript
AngularJS 事件发布机制
2018/08/28 Javascript
微信小程序利用Canvas绘制图片和竖排文字详解
2019/06/25 Javascript
vue中配置scss全局变量的步骤
2020/12/28 Vue.js
Python中文分词实现方法(安装pymmseg)
2016/06/14 Python
python和ruby,我选谁?
2017/09/13 Python
python MySQLdb使用教程详解
2018/03/20 Python
Python 实现「食行生鲜」签到领积分功能
2018/09/26 Python
docker django无法访问redis容器的解决方法
2019/08/21 Python
PYTHON实现SIGN签名的过程解析
2019/10/28 Python
浅谈python量化 双均线策略(金叉死叉)
2020/06/03 Python
Python接收手机短信的代码整理
2020/08/02 Python
Python实现扫码工具的示例代码
2020/10/09 Python
X/HTML5 和 XHTML2
2008/10/17 HTML / CSS
西班牙英格列斯百货法国官网:El Corte Inglés法国
2017/07/09 全球购物
瑞典网上购买现代和复古家具:Reforma
2019/10/21 全球购物
卫校中专生个人自我评价
2013/09/19 职场文书
校长岗位职责
2013/11/26 职场文书
李敖北大演讲稿
2014/05/24 职场文书
停车位租赁协议书
2014/09/24 职场文书
辞职信模板(中英文版)
2015/02/27 职场文书
办公室卫生管理制度
2015/08/04 职场文书