python笔记-集合类型

一、集合类型

set 对象是由具有唯一性的 hashable 对象所组成的无序多项集。

  • 常见的用途包括成员检测
  • 从序列中去除重复项
  • 数学中的集合类计算,例如交集、并集、差集与对称差集等等
  • 与其他多项集一样,集合也支持 x in set, len(set) 和 for x in set
  • 作为一种无序的多项集,集合并不记录元素位置或插入顺序。 相应地,集合不支持索引、切片或其他序列类的操作

集合对象在python中内置两种: set 和 frozenset

  1. set: 类型是可变的, 由于是可变类型,它没有哈希值,且不能被用作字典的键或其他集合的元素
  2. frozenset: 类型是不可变, 内容在被创建后不能再改变;因此它可以被用作字典的键或其他集合的元素

创建方式

  1. set([iterable]): 返回一个新的 set 对象,其元素来自于 iterable
    • 除了可以使用 set 构造器,非空的 set (不是 frozenset) 还可以通过将以逗号分隔的元素列表包含于花括号之内来创建,例如: {'jack', 'sjoerd'}
  2. frozenset([iterable]): 返回一个新的 frozenset 对象,其元素来自于 iterable

二、set和frozenset共同的方法

  1. len(s): 返回集合 s 中的元素数量(即 s 的基数)。
  2. x in s: 检测 x 是否为 s 中的成员。
  3. x not in s: 检测 x 是否非 s 中的成员。
  4. [frozen]set.isdisjoint(other): [frozen]set与other不相交返回 Ture
>>> se1 = {1,2,3,4}
>>> se2 = {2,3,4,5,6,7}
>>> se1.isdisjoint(se2)
False
  1. [frozen]set.issubset(other), set <= other: [集合是否为other的子集]检测是否集合中的每个元素都在 other 之中。
    • set < other: 检测集合是否为 other 的真子集,即 set <= other and set != other
>>> se1 = {1,2,3,4}
>>> se2 = {2,3,4,5,6,7}
>>> se1.issubset(se2)
False
  1. [frozen]set.issuperset(other), set >= other: [other是否为集合的子集]检测是否 other 中的每个元素都在集合之中。
    • set > other: 检测集合是否为 other 的真超集,即 set >= other and set != other。
>>> se1 = {1, 2, 3, 4}
>>> se2 = {2, 3, 4, 5, 6, 7}
>>> se1.issuperset(se2)
False
  1. [frozen]set.union(*others), set | other | ...: [并集]返回一个新集合,其中包含来自原集合以及 others 指定的所有集合中的元素。
False
>>> se1 = {1, 2, 3, 4}
>>> se2 = {2, 3, 4, 5, 6, 7}
>>> se1|se2
{1, 2, 3, 4, 5, 6, 7}
>>> se1.union(se2)
{1, 2, 3, 4, 5, 6, 7}
  1. [frozen]set.intersection(*others), set & other & ...: [交集]返回一个新集合,其中包含原集合以及 others 指定的所有集合中共有的元素。
>>> se1 = {1, 2, 3, 4}
>>> se2 = {2, 3, 4, 5, 6, 7}
>>> se1 & se2
{2, 3, 4}
>>> se1.intersection(se2)
{2, 3, 4}
  1. [frozen]set.difference(*others), set - other - ...: [set对others的补集]返回一个新集合,其中包含原集合中在 others 指定的其他集合中不存在的元素。
>>> se1 = {1, 2, 3, 4}
>>> se2 = {2, 3, 4, 5, 6, 7}
>>> se1.difference(se2)
{1}
>>> se2.difference(se1)
{5, 6, 7}
>>> se1 - se2
{1}
>>> se2 - se1
{5, 6, 7}
  1. [frozen]set.symmetric_difference(other), set ^ other: [对称差]返回一个新集合,其中的元素或属于原集合或属于 other 指定的其他集合,但不能同时属于两者。
>>> se1 = {1, 2, 3, 4}
>>> se2 = {2, 3, 4, 5, 6, 7}
>>> se1 ^ se2
{1, 5, 6, 7}
>>> se2 ^ se1
{1, 5, 6, 7}
>>> se1.symmetric_difference(se2)
{1, 5, 6, 7}
  1. [frozen]set.copy(): 返回原集合的浅拷贝

三、set独有方法

  1. set.update(*others), set |= other | ...: [并集]更新集合,添加来自 others 中的所有元素
>>> se = {"hello", "python", "java"}
>>> se.update({"c"})  # update传入的参数也是集合
>>> se
{'python', 'hello', 'java', 'c'}
  1. set.intersection_update(*others), set &= other & ...: [交集]更新集合,只保留其中在所有 others 中也存在的元素
>>> se = {'python', 'hello', 'java', 'c'}
>>> se.intersection_update({"c"})
>>> se
{'c'}
  1. set.difference_update(*others), set -= other | ...: [补集]更新集合,移除其中也存在于 others 中的元素
>>> se = {'python', 'hello', 'java'}
>>> se.difference_update({"hello", "c"})
>>> se
{'python', 'java'}
  1. set.symmetric_difference_update(other), set ^= other: [对称差]更新集合,只保留存在于集合的一方而非共同存在的元素
>>> se = {"hello", "python", "java"}
>>> se.symmetric_difference_update({"java", "c", "hello"})
>>> se
{'python', 'c'}
  1. set.add(elem): 将元素 elem 添加到集合中。
>>> se = {"java", "c", "python"}
>>> se.add("javascript")
>>> se
{'python', 'javascript', 'java', 'c'}
  1. set.remove(elem): 从集合中移除元素 elem。 如果 elem 不存在于集合中则会引发 KeyError
>>> se = {'python', 'javascript', 'java', 'c'}
>>> se.remove("javascript")
>>> se
{'python', 'java', 'c'}
  1. set.discard(elem): 如果元素 elem 存在于集合中则将其移除
>>> se = {'python', 'java', 'c'}
>>> se.discard("hello")  # 不存在
>>> se
{'python', 'java', 'c'}
>>> se.discard("c")  # 存在
>>> se
{'python', 'java'}
  1. set.pop(): 从集合中移除并返回任意一个元素。 如果集合为空则会引发 KeyError
>>> se = {'python', 'java'}
>>> se.pop()
'python'
  1. set.clear(): 从集合中移除所有元素
原文地址:https://www.cnblogs.com/duyupeng/p/13063146.html