Python集合之set和frozenset的使用详解

2022-07-15,,,,

简介

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

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

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}

无交集、子集、超集

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

运算符

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的使用详解的详细内容,更多关于python集合set frozenset的资料请关注其它相关文章!

《Python集合之set和frozenset的使用详解.doc》

下载本文的Word格式文档,以方便收藏与打印。