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字符遍历的艺术
Sep 06 Python
重命名批处理python脚本
Apr 05 Python
Python对象的深拷贝和浅拷贝详解
Aug 25 Python
Python复数属性和方法运算操作示例
Jul 21 Python
Anaconda多环境多版本python配置操作方法
Sep 12 Python
在python3.5中使用OpenCV的实例讲解
Apr 02 Python
python 读取txt,json和hdf5文件的实例
Jun 05 Python
Python双向循环链表实现方法分析
Jul 30 Python
Python中注释(多行注释和单行注释)的用法实例
Aug 28 Python
Python字典的概念及常见应用实例详解
Oct 30 Python
Python 静态方法和类方法实例分析
Nov 21 Python
用python制作个视频下载器
Feb 01 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
Zend引擎的发展 [15]
2006/10/09 PHP
在Windows中安装Apache2和PHP4的权威指南
2006/10/09 PHP
yii2中关于加密解密的那些事儿
2018/06/12 PHP
ThinkPHP5.0多个文件上传后找不到临时文件的修改方法
2018/07/30 PHP
在IE下获取object(ActiveX)的Param的代码
2009/09/15 Javascript
bgsound 背景音乐 的一些常用方法及特殊用法小结
2010/05/11 Javascript
FireFox下XML对象转化成字符串的解决方法
2011/12/09 Javascript
Javascript 面向对象(二)封装代码
2012/05/23 Javascript
5秒后跳转效果(setInterval/SetTimeOut)
2013/05/03 Javascript
javascript dom追加内容实现示例
2013/09/21 Javascript
jQuery实现返回顶部功能适合不支持js的浏览器
2014/08/19 Javascript
javascript实现禁止复制网页内容
2014/12/16 Javascript
Javascript 拖拽雏形中的一些问题(逐行分析代码,让你轻松了拖拽的原理)
2015/01/23 Javascript
javascript实现图片自动和可控的轮播切换特效
2015/04/13 Javascript
浅谈JavaScript 的执行顺序
2015/08/07 Javascript
jQuery实现可拖拽的许愿墙效果【附demo源码下载】
2016/09/14 Javascript
AngularJS extend用法详解及实例代码
2016/11/15 Javascript
js实现时间轴自动排列效果
2017/03/09 Javascript
ES6 Iterator接口和for...of循环用法分析
2019/07/31 Javascript
vue移动端使用canvas签名的实现
2020/01/15 Javascript
js实现上传按钮并显示缩略图小轮子
2020/05/04 Javascript
[02:49]DOTA2完美大师赛首日观众采访
2017/11/23 DOTA
[03:12]完美世界DOTA2联赛PWL DAY7集锦
2020/11/06 DOTA
python使用urllib2模块获取gravatar头像实例
2013/12/18 Python
python中引用与复制用法实例分析
2015/06/04 Python
Python中定时任务框架APScheduler的快速入门指南
2017/07/06 Python
基于Python闭包及其作用域详解
2017/08/28 Python
python使用pil库实现图片合成实例代码
2018/01/20 Python
Python 获取numpy.array索引值的实例
2019/12/06 Python
若干个Java基础面试题
2015/05/19 面试题
手工社团活动方案
2014/02/17 职场文书
励志演讲稿200字
2014/08/21 职场文书
2016年教师节慰问信
2015/12/01 职场文书
趣味运动会口号
2015/12/24 职场文书
Java 泛型详解(超详细的java泛型方法解析)
2021/07/02 Java/Android
Nginx工作模式及代理配置的使用细节
2022/03/21 Servers