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比较2个xml内容的方法
May 11 Python
Python中的if、else、elif语句用法简明讲解
Mar 11 Python
运行django项目指定IP和端口的方法
May 14 Python
一文带你了解Python中的字符串是什么
Nov 20 Python
Python目录和文件处理总结详解
Sep 02 Python
tensorflow 限制显存大小的实现
Feb 03 Python
基于python爬取有道翻译过程图解
Mar 31 Python
pymysql之cur.fetchall() 和cur.fetchone()用法详解
May 15 Python
Python 实现自动登录+点击+滑动验证功能
Jun 10 Python
在Mac中配置Python虚拟环境过程解析
Jun 22 Python
通过代码实例了解Python sys模块
Sep 14 Python
Python函数中apply、map、applymap的区别
Nov 27 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中3种方法统计字符串中每种字符的个数并排序
2012/08/27 PHP
PHP+jQuery+Ajax实现用户登录与退出
2015/04/27 PHP
WordPress的文章自动添加关键词及关键词的SEO优化
2016/03/01 PHP
golang与php实现计算两个经纬度之间距离的方法
2016/07/22 PHP
详谈PHP面向对象中常用的关键字和魔术方法
2017/02/04 PHP
formvalidator验证插件中有关ajax验证问题
2013/01/04 Javascript
jquery插件bxslider用法实例分析
2015/04/16 Javascript
JQuery通过AJAX从后台获取信息显示在表格上并支持行选中
2015/09/15 Javascript
JavaScript 经典实例日常收集整理(常用经典)
2016/03/30 Javascript
Yarn的安装与使用详细介绍
2016/10/25 Javascript
jQuery Password Validation密码验证
2016/12/30 Javascript
webpack配置的最佳实践分享
2017/04/21 Javascript
[01:05:12]2014 DOTA2国际邀请赛中国区预选赛 TongFu VS CIS-GAME
2014/05/21 DOTA
[02:32]【DOTA2亚洲邀请赛】iceice,梦开始的地方
2017/03/13 DOTA
使用cx_freeze把python打包exe示例
2014/01/24 Python
Python Tkinter基础控件用法
2014/09/03 Python
Python bsddb模块操作Berkeley DB数据库介绍
2015/04/08 Python
python批量制作雷达图的实现方法
2016/07/26 Python
Win8下python3.5.1安装教程
2020/07/29 Python
使用Python控制摄像头拍照并发邮件
2019/04/23 Python
python爬虫之自制英汉字典
2019/06/24 Python
Django1.11配合uni-app发起微信支付的实现
2019/10/12 Python
python闭包、深浅拷贝、垃圾回收、with语句知识点汇总
2020/03/11 Python
详解Windows下PyCharm安装Numpy包及无法安装问题解决方案
2020/06/18 Python
Python离线安装各种库及pip的方法
2020/11/28 Python
HTML5之SVG 2D入门12—SVG DOM及DOM操作介绍
2013/01/30 HTML / CSS
10个最常见的HTML5面试题 附答案
2016/06/06 HTML / CSS
康帕斯酒店预订:Compass Hospitality(支持中文)
2018/08/23 全球购物
英国婴儿及儿童产品商店:TigerParrot
2019/03/04 全球购物
FirstCry阿联酋儿童和婴儿产品网上购物:FirstCry.ae
2021/02/22 全球购物
银行类自荐信
2014/02/04 职场文书
竞选班长演讲稿400字
2014/08/22 职场文书
办公用房租赁协议书
2014/11/29 职场文书
2014学生会工作总结报告
2014/12/02 职场文书
法务专员岗位职责
2015/02/14 职场文书
军训个人总结
2015/03/03 职场文书