User:Xyy23330121/Python/集合

来自维基学院


Python 中,集合是无序的、由多个不同元素组成的结构。其中 set 是可变的,而 frozenset 是不可变的。因此,frozenset 可以作为字典的键,而 set 不能。

当尝试向集合添加集合中已有的元素,或者在创建集合时使用了重复的元素。则只会保留重复元素中的一个,其余的会被忽略。

集合是可迭代对象,即便它没有明确的顺序。进行迭代时,它会按照一个随机的顺序迭代元素,直至所有元素都进行过迭代为止。

创建集合[编辑 | 编辑源代码]

Python 创建集合主要有三种方式:

  1. 字面格式:newSet = {'str', 1, False}
  2. 推导式:newSet = {c for c in 'Python' if c not in 'py'}
  3. set函数:对任意可迭代对象 iterable ,用 set(iterable)frozenset(iterable)
    特别的,可以用 set()frozenset() 创建空字典。

嵌套[编辑 | 编辑源代码]

列表、元组等结构也可以作为集合的元素出现。特别的,集合也可以作为集合的元素。

集合的操作[编辑 | 编辑源代码]

集合元素数量[编辑 | 编辑源代码]

len(s)[编辑 | 编辑源代码]

返回集合 s 中元素的数量。

属于 / 不属于[编辑 | 编辑源代码]

in / not in[编辑 | 编辑源代码]

可以用 x in sx not in s 判断元素是否在集合中。比较运算符可以串联。

含于 / 真含于[编辑 | 编辑源代码]

s <= other[编辑 | 编辑源代码]

如果集合 s 含于集合 other 则返回 True。反之返回 False。比较运算符可以串联。

s.issubset(other)[编辑 | 编辑源代码]

s <= other。但此时 other 可以为任何可用 set 函数或 frozenset 函数转变为集合的类型。

s < other[编辑 | 编辑源代码]

如果集合 s 真含于集合 other 则返回 True。反之返回 False。比较运算符可以串联。

s >= other[编辑 | 编辑源代码]

如果集合 other 含于集合 s 则返回 True。反之返回 False。比较运算符可以串联。

s.issuperset(other)[编辑 | 编辑源代码]

s >= other。但此时 other 可以为任何可用 set 函数或 frozenset 函数转变为集合的类型。

s > other[编辑 | 编辑源代码]

如果集合 other 真含于集合 s 则返回 True。反之返回 False。比较运算符可以串联。

交集[编辑 | 编辑源代码]

s.isdisjoint(other)[编辑 | 编辑源代码]

如果集合 s 和集合 other 交集为空,则返回 True。反之返回 False

s & other & ...[编辑 | 编辑源代码]

返回该表达式中所有集合的交集。返回的集合是 set 还是 frozenset,取决于 s 的类型。

s.intersection(other, ...)[编辑 | 编辑源代码]

s & other & ...。但此时 other 可以为任何可用 set 函数或 frozenset 函数转变为集合的类型。

并集[编辑 | 编辑源代码]

s | other | ...[编辑 | 编辑源代码]

返回该表达式中所有集合的并集。返回的集合是 set 还是 frozenset,取决于 s 的类型。

s.union(other, ...)[编辑 | 编辑源代码]

s | other | ...。但此时 other 可以为任何可用 set 函数或 frozenset 函数转变为集合的类型。

集合的差[编辑 | 编辑源代码]

s - other - ...[编辑 | 编辑源代码]

如果集合 s 和其它所有集合 other, ... 的差。即:返回一个新集合,该集合包含在 s 中、但不在 other | ... 中的元素。

返回的集合是 set 还是 frozenset,取决于 s 的类型。

s.difference(other, ...)[编辑 | 编辑源代码]

s - other - ...。但此时 other 可以为任何可用 set 函数或 frozenset 函数转变为集合的类型。

集合的对称差[编辑 | 编辑源代码]

s ^ other[编辑 | 编辑源代码]

返回一个集合,该集合中的元素属于 s.union(other) 但不属于 s.difference(other)

返回的集合是 set 还是 frozenset,取决于 s 的类型。

s.symmetric_difference(other)[编辑 | 编辑源代码]

s ^ other。但此时 other 可以为任何可用 set 函数或 frozenset 函数转变为集合的类型。

s.copy()[编辑 | 编辑源代码]

返回集合 s 的拷贝。

可变集合的特有操作[编辑 | 编辑源代码]

以下操作中,s 均为 set 对象。而不能是 frozenset 对象

赋值并[编辑 | 编辑源代码]

s |= other | ...[编辑 | 编辑源代码]

在集合 s 中添加所有属于 other | ... 的元素。

s.update(other, ...)[编辑 | 编辑源代码]

s |= other | ...。但此时 other 可以为任何可用 set 函数或 frozenset 函数转变为集合的类型。

赋值交[编辑 | 编辑源代码]

s &= other & ...[编辑 | 编辑源代码]

从集合 s 中移除所有不属于 other & ... 的元素。

s.intersection_update(other, ...)[编辑 | 编辑源代码]

s &= other & ...。但此时 other 可以为任何可用 set 函数或 frozenset 函数转变为集合的类型。

赋值差[编辑 | 编辑源代码]

s -= other | ...[编辑 | 编辑源代码]

从集合 s 中移除所有属于 other | ... 的元素。

s.difference_update(other, ...)[编辑 | 编辑源代码]

s -= other | ...。但此时 other 可以为任何可用 set 函数或 frozenset 函数转变为集合的类型。

赋值对称差[编辑 | 编辑源代码]

s ^= other[编辑 | 编辑源代码]

更新 s ,使其等于 s ^ other

s.symmetric_difference_update(other)[编辑 | 编辑源代码]

s ^= other。但此时 other 可以为任何可用 set 函数或 frozenset 函数转变为集合的类型。

添加 / 删除元素[编辑 | 编辑源代码]

s.add(elem)[编辑 | 编辑源代码]

elem 添加到集合 s 中。

s.remove(elem)[编辑 | 编辑源代码]

elem 从集合 s 中移除,如果集合中没有 elem,则报错 KeyError

s.iscard(elem)[编辑 | 编辑源代码]

elem 从集合 s 中移除。如果集合中没有 elem,则不做任何操作。

s.pop(elem)[编辑 | 编辑源代码]

从集合 s 中任意移除一个元素并返回。如果集合为空,则报错 KeyError

s.clear()[编辑 | 编辑源代码]

将集合 s 变为空集。