集合类型 : set, frozenset
集合
set 对象是由具有唯一性的 hashable 对象所组成的无序多项集。
常见的用途包括成员检测、从序列中去除重复项以及数学中的集合类计算,例如交集、并集、差集与对称差集等等。
与其他多项集一样,集合也支持 x in set
, len(set)
和 for x in set
。 作为一种无序的多项集,集合并不记录元素位置或插入顺序。 相应地,集合不支持索引、切片或其他序列类的操作。
目前有两种内置集合类型,set 和 frozenset。
set
set 类型是可变的 --- 其内容可以使用 add()
和 remove()
这样的方法来改变。 由于是可变类型,它没有哈希值,且不能被用作字典的键或其他集合的元素。
除了可以使用 set 构造器,非空的 set (不是 frozenset) 还可以通过将以逗号分隔的元素列表包含于花括号之内来创建,例如: {'jack', 'sjoerd'}
。
class set
([iterable])
返回一个新的 set 对象,其元素来自于 iterable。 集合的元素必须为 hashable。 如果未指定 iterable,则将返回一个新的空集合。
集合可用多种方式来创建:
使用花括号内以逗号分隔元素的方式:
{'jack', 'sjoerd'}
使用集合推导式:
{c for c in 'abracadabra' if c not in 'abc'}
使用类型构造器:
set()
,set('foobar')
,set(['a', 'b', 'foo'])
frozenset
frozenset 类型是不可变并且为 hashable --- 其内容在被创建后不能再改变;因此它可以被用作字典的键或其他集合的元素。
class frozenset
([iterable])
返回一个新的 frozenset 对象,这是一个冻结的集合,冻结后集合不能再添加或删除任何元素。其元素来自于 iterable。 集合的元素必须为 hashable。 要表示由集合对象构成的集合,所有的内层集合必须为 frozenset 对象。 如果未指定 iterable,则将返回一个新的空集合。
集合可用多种方式来创建:
使用花括号内以逗号分隔元素的方式:
{'jack', 'sjoerd'}
使用集合推导式:
{c for c in 'abracadabra' if c not in 'abc'}
使用类型构造器:
set()
,set('foobar')
,set(['a', 'b', 'foo'])
方法
len(s)
返回集合 s 中的元素数量(即 s 的基数)。
set1 = {'python', 'version', 'windows'}
print(set1)
# output: {'python', 'windows', 'version'}
print(len(set1))
# output: 3
frozenset1 = frozenset({'python', 'version', 'windows'})
print(frozenset1)
# output: frozenset({'python', 'windows', 'version'})
print(len(frozenset1))
# output: 3
x in s
检测 x 是否为 s 中的成员。
x not in s
检测 x 是否非 s 中的成员。
set1 = {'python', 'version', 'windows'}
print(set1)
# output: {'windows', 'version', 'python'}
print('python' in set1)
# output: True
frozenset1 = frozenset({'python', 'windows', 'version'})
print(frozenset1)
# output: frozenset({'windows', 'version', 'python'})
print('c' not in frozenset1)
# output: True
isdisjoint
(other)
如果集合中没有与 other 共有的元素则返回 True
。 当且仅当两个集合的交集为空集合时,两者为不相交集合。
set1 = {'python', 'version', 'windows'}
print(set1)
# output: {'windows', 'python', 'version'}
print(set1.isdisjoint('c'))
# output: True
frozenset1 = frozenset({'python', 'version', 'windows'})
print(frozenset1)
# output: frozenset({'windows', 'python', 'version'})
print(set1.isdisjoint(frozenset1))
# output: False
issubset
(other)
set <= other
检测是否集合中的每个元素都在 other 之中。
set < other
检测集合是否为 other 的真子集,即 set <= other and set != other
。
set1 = {'python', 'version', 'windows'}
set2 = {'python', 'version' }
print(set1)
# output: {'python', 'version', 'windows'}
print(set2)
# output: {'python', 'version'}
print(set1.issubset(set2))
# output: False
frozenset1 = frozenset({'python', 'version', 'windows'})
print(frozenset1)
# output: frozenset({'python', 'version', 'windows'})
print(set2.issubset(frozenset1))
# output: True
print(set1.issubset(frozenset1))
# output: True
issuperset
(other)
set >= other
检测是否 other 中的每个元素都在集合之中。
set > other
检测集合是否为 other 的真超集,即 set >= other and set != other
。
set1 = {'python', 'version', 'windows'}
set2 = {'python', 'version' }
print(set1)
# output: {'python', 'version', 'windows'}
print(set2)
# output: {'python', 'version'}
print(set1.issuperset(set2))
# output: False
frozenset1 = frozenset({'python', 'version', 'windows'})
print(frozenset1)
# output: frozenset({'python', 'version', 'windows'})
print(set2.issuperset(frozenset1))
# output: False
print(set1.issuperset(frozenset1))
# output: True
union
(*others)
set | other | ...
返回一个新集合,其中包含来自原集合以及 others 指定的所有集合中的元素。
set1 = {'python', 'version', 'windows'}
set2 = {'jim', 'study'}
print(set1)
# output: {'windows', 'python', 'version'}
print(set2)
# output: {'jim', 'study'}
print(set1.union(set2))
# output: {'windows', 'jim', 'version', 'python', 'study'}
frozenset1 = frozenset({'python', 'version', 'windows'})
print(frozenset1)
# output: frozenset({'windows', 'python', 'version'})
print(frozenset1.union(set2))
# output: frozenset({'windows', 'jim', 'version', 'python', 'study'})
intersection
(*others)
set & other & ...
返回一个新集合,其中包含原集合以及 others 指定的所有集合中共有的元素。
set1 = {'python', 'version', 'windows'}
set2 = {'python', 'study'}
print(set1)
# output: {'python', 'windows', 'version'}
print(set2)
# output: {'python', 'study'}
print(set1.intersection(set2))
# output:{'python'}
frozenset1 = frozenset({'python', 'version', 'windows'})
print(frozenset1)
# output: frozenset({'python', 'windows', 'version'})
print(frozenset1.intersection(set2))
# output: frozenset({'python'})
difference
(*others)
set - other - ...
返回一个新集合,其中包含原集合中在 others 指定的其他集合中不存在的元素。
set1 = {'python', 'version', 'windows'}
set2 = {'python', 'study'}
print(set1)
# output: {'windows', 'version', 'python'}
print(set2)
# output: {'study', 'python'}
print(set1.difference(set2))
# output: {'windows', 'version'}
frozenset1 = frozenset({'python', 'version', 'windows'})
print(frozenset1)
# output: frozenset({'windows', 'version', 'python'})
print(frozenset1.difference(set2))
# output: frozenset({'windows', 'version'})
symmetric_difference
(other)
set ^ other
返回一个新集合,其中的元素或属于原集合或属于 other 指定的其他集合,但不能同时属于两者。
set1 = {'python', 'version', 'windows'}
set2 = {'python', 'study'}
print(set1)
# output: {'python', 'version', 'windows'}
print(set2)
# output: {'study', 'python'}
print(set1.symmetric_difference(set2))
# output: {'study', 'version', 'windows'}
frozenset1 = frozenset({'python', 'version', 'windows'})
print(frozenset1)
# output: frozenset({'python', 'version', 'windows'})
print(frozenset1.symmetric_difference(set2))
# output: frozenset({'study', 'version', 'windows'})
copy
()
返回原集合的浅拷贝。
set1 = {'python', 'version', 'windows'}
set2 = set1.copy()
print(set1)
# output: {'python', 'version', 'windows'}
print(set2)
# output: {'python', 'version', 'windows'}
set1 = set1.union({'study'})
print(set1)
# output: {'python', 'version', 'study', 'windows'}
print(set2)
# output: {'python', 'version', 'windows'}
frozenset1 = frozenset({'python', 'version', 'windows'})
print(frozenset1)
# output: frozenset({'python', 'version', 'windows'})
frozenset2 = frozenset1.copy()
print(frozenset2)
# output: frozenset({'python', 'version', 'windows'})
frozenset1 = frozenset1.union({'study'})
print(frozenset1)
# output: frozenset({'python', 'version', 'study', 'windows'})
print(frozenset2)
# output: frozenset({'python', 'version', 'windows'})
注意, union() 、 intersection() 、 difference() 、 symmetric_difference() 、 issubset() 和 issuperset() 方法的非运算符版本可以接受任何可迭代对象作为一个参数。相比之下,基于运算符的对应方法则要求参数为集合对象。这就避开了像 set('abc') & 'cbs'
这样容易出错的结构,而换成了可读性更好的 set('abc').intersection('cbs')
。
set 和 frozenset 均支持集合与集合的比较。 两个集合当且仅当每个集合中的每个元素均包含于另一个集合之内(即各为对方的子集)时则相等。 一个集合当且仅当其为另一个集合的真子集(即为后者的子集但两者不相等)时则小于另一个集合。 一个集合当且仅当其为另一个集合的真超集(即为后者的超集但两者不相等)时则大于另一个集合。
set 的实例与 frozenset 的实例之间基于它们的成员进行比较。 例如 set('abc') == frozenset('abc')
返回 True
,set('abc') in set([frozenset('abc')])
也一样。
子集与相等比较并不能推广为完全排序函数。 例如,任意两个非空且不相交的集合不相等且互不为对方的子集,因此以下 所有 比较均返回 False
: a<b
, a==b
, or a>b
。
由于集合仅定义了部分排序(子集关系),因此由集合构成的列表 list.sort() 方法的输出并无定义。
集合的元素,与字典的键类似,必须为 hashable。
混合了 set 实例与 frozenset 的二进制位运算将返回与第一个操作数相同的类型。例如: frozenset('ab') | set('bc')
将返回 frozenset 的实例。
可用于 set 而不能用于不可变的 frozenset 实例的操作
update
(*others)
set |= other | ...
更新集合,添加来自 others 中的所有元素。
set1 = {'python', 'version', 'windows'}
print(set1)
# output: {'python', 'version', 'windows'}
set1.update({'study'})
print(set1)
# output: {'study', 'python', 'windows', 'version'}
intersection_update
(*others)
set &= other & ...
更新集合,只保留其中在所有 others 中也存在的元素。
set1 = {'python', 'version', 'windows'}
print(set1)
# output: {'python', 'version', 'windows'}
set1.intersection_update({'python'})
print(set1)
# output: {'python'}
difference_update
(*others)
set -= other | ...
更新集合,移除其中也存在于 others 中的元素。
set1 = {'python', 'version', 'windows'}
print(set1)
# output: {'python', 'version', 'windows'}
set1.difference_update({'python'})
print(set1)
# output: {'version', 'windows'}
symmetric_difference_update
(other)
set ^= other
更新集合,只保留存在于集合的一方而非共同存在的元素。
set1 = {'python', 'version', 'windows'}
print(set1)
# output: {'python', 'version', 'windows'}
set1.symmetric_difference_update({'python', 'Os'})
print(set1)
# output: {'version', 'windows', 'Os'}
add
(elem)
将元素 elem 添加到集合中。
remove
(elem)
从集合中移除元素 elem。 如果 elem 不存在于集合中则会引发 KeyError。
set1 = {'python', 'version', 'windows'}
print(set1)
# output: {'python', 'version', 'windows'}
for item in ['study', 'Os']:set1.add(item)
print(set1)
# output: {'study', 'Os', 'python', 'version', 'windows'}
for item in ['study', 'Os']:set1.remove(item)
print(set1)
# output: {'python', 'version', 'windows'}set1.remove('item')
print(set1)
# output: Traceback (most recent call last): File "E:\PyCode\Python_Study\LsitDemo.py", line 13, in <module> set1.remove('item') KeyError: 'item'
discard
(elem)
如果元素 elem 存在于集合中则将其移除。
set1 = {'python', 'version', 'windows'}
print(set1)
# output: {'python', 'version', 'windows'}
for item in ['study', 'Os']:set1.add(item)
print(set1)
# output: {'version', 'Os', 'study', 'python', 'windows'}
for item in ['study', 'Os']:set1.discard(item)
print(set1)
# output: {'version', 'python', 'windows'}
set1.discard('Os')
print(set1)
# output: {'version', 'python', 'windows'}
pop
()
从集合中移除并返回任意一个元素。 如果集合为空则会引发 KeyError。
set1 = {'python', 'version', 'windows'}
print(set1)
# output: {'python', 'version', 'windows'}
print(set1.pop())
# output: python
print(set1)
# output: {'version', 'windows'}
clear
()
从集合中移除所有元素。
set1 = {'python', 'version', 'windows'}
print(set1)
# output: {'python', 'version', 'windows'}
set1.clear()
print(set1)
# output: set()
请注意,非运算符版本的 update(), intersection_update(), difference_update() 和 symmetric_difference_update() 方法将接受任意可迭代对象作为参数。
请注意,__contains__()
, remove() 和 discard() 方法的 elem 参数可能是一个 set。 为支持对一个等价的 frozenset 进行搜索,会根据 elem 临时创建一个该类型对象。
集合类型 : set, frozenset相关推荐
- python:集合类型 --- set, frozenset
python:集合类型 --- set, frozenset set 对象是由具有唯一性的 hashable 对象所组成的无序多项集. 常见的用途包括成员检测.从序列中去除重复项以及数学中的集合类计算 ...
- 集合类型--- set, frozenset
目录 集合类型--- set, frozenset set 和frozenset 的实例提供以下操作: len(s) x in s x not in s isdisjoint(other):用于判断两 ...
- 【Python3】【碎碎念】集合类型,set和frozenset
目录 1. 前言 2. 简介 3. 常用操作 3.0 操作速览表 3.1 用于 set 和 frozenset 的操作 3.1.1 创建 set 和 frozenset -- set() frozen ...
- [Python] Python数据类型(三)集合类型之set集合和frozenset集合
1.集合基本概念 集合是0-n个对象引用的无序组合,集合中存储的元素不可重复,因此这些对象引用的引用的对象必须是可哈希运算的 . 1.python中提供了两种内置的集合类型:可变的set类型和固定的f ...
- python中的集合set类型是可变的吗_[Python] Python数据类型(三)集合类型之set集合和frozenset集合...
1.集合基本概念 集合是0-n个对象引用的无序组合,集合中存储的元素不可重复,因此这些对象引用的引用的对象必须是可哈希运算的 . 1.python中提供了两种内置的集合类型:可变的set类型和固定的 ...
- Python回顾与整理5:映像和集合类型
0.说明 依然是按照前面介绍的几种序列类型的思路来总结字映像类型和集合类型,即先做一个介绍,再讨论可用操作符.工厂函数.内建函数和方法. 1.映射类型:字典 映射类型被称做哈希表,而Python中的字 ...
- python集合类型的四种操作符_深入解析Python中的集合类型操作符
(1)标准类型操作符(所有的集合类型) 成员关系 (in, not in) 就序列而言,Python中的in和not in操作符决定某个元素是否是一个集合中的成员. 集合等价/不等价 等价/不等价被用 ...
- python映射类型-python2.x学习笔记(8)-映射和集合类型
1. 字典是 Python 语言中唯一的映射类型.映射类型对象里哈希值(键) 和指向的对象(值)是一对一的关系. 2. 一个字典对象是可变的,它是一个容器类型,能存储任意个数的 Python 对象,其 ...
- python中的集合类型_Python中的集合类型知识讲解
>>> s.add('z') >>> s set(['c', 'e', 'h', 'o', 'p', 's', 'z']) >>> s.updat ...
最新文章
- 基于stm32的can总线彻底研究
- java五子棋课程设计_JAVA课程设计(五子棋)021
- CNN训练模型 花卉
- redisb并发访问慢出现的问题
- 2021年7月国产数据库排行榜:openGauss成绩依旧亮眼,Kingbase向Top 10发起冲刺
- 第三:项目框架设计介绍
- 人才第一!英伟达大幅扩大深度学习学院(DLI)规模
- BCGControlBar教程:可视化管理器
- codeforces 796A-D
- 深入研究RocketMQ生产者发送消息的底层原理
- IDEA 文件对比功能图解
- linux切换声卡,Ubuntu中双声卡使用实例
- Google Code Prettify,代码高亮的JS库
- (转)屈子:独立人格可与日月争光
- Ubuntu双系统卸载教程
- 天行健,君子以自强不息;地势坤,君子以厚德载物
- androidsettitle方法_在Android应用程序中,Toolbar.setTitle方法无效 - 应用程序名称显示为ti...
- 贴片电容的封装尺寸容值解读
- 谈谈以前端角度出发做好SEO需要考虑什么?
- 趣学算法14天阅读|Day1