Skip to main content

集合

基础使用

创建集合

  • 通过列表创建
s  = set([1,2,3,3,3,3,3])
  • 直接创建
s = {1,2,3,4,5,5,5,}

类型判断

判断是否是一个集合


关键 API

创建集合说明
set(x)Create a set containing the elements of the collection “x”.<br/>
frozenset(x)Create an immutable set containing the elements of the collection “x”.<br/>返回一个冻结的集合,冻结后集合不能再添加或删除任何元素。
管理集合说明
S.add(x)Add “x” to the set.
S.update(s)Add all elements of sequence “s” to the set.
S.remove(x)Remove “x” from the set. If “x” is not present, this method raises a LookupError exception.
S.discard(x)Remove “x” from the set if it is present, or do nothing if it is not.
S.pop()Remove and return an arbitrary element, raising a LookupError if the element is not present.
S.clear()Remove all elements from this set.
S.copy()Make a new set.
s.issuperset()Check for a superset relationship.
s.issubset()Check for a subset relationship.