python 内置数据结构的基本操作 —— Set(2)
2017-05-09 10:02
387 查看
class set([iterable]) class frozenset([iterable])
Return a new set or frozenset object whose elements are taken from iterable. The elements of a set must be hashable. To represent sets of sets, the inner sets must be frozenset objects. If iterable is not specified, a new empty set is returned.
Instances of set and frozenset provide the following operations:
len(s)
Return the number of elements in set s (cardinality of s).
x in s
Test x for membership in s.
x not in s
Test x for non-membership in s.
isdisjoint(other)
Return True if the set has no elements in common with other. Sets are disjoint if and only if their intersection is the empty set.
issubset(other)
set <= other
Test whether every element in the set is in other.
set < other
Test whether the set is a proper subset of other, that is, set <= other and set != other.
issuperset(other)
set >= other
Test whether every element in other is in the set.
set > other
Test whether the set is a proper superset of other, that is, set >= other and set != other.
union(*others)
set | other | ...
Return a new set with elements from the set and all others.
intersection(*others)
set & other & ...
Return a new set with elements common to the set and all others.
difference(*others)
set - other - ...
Return a new set with elements in the set that are not in the others.
symmetric_difference(other)
set ^ other
Return a new set with elements in either the set or other but not both.
copy()
Return a new set with a shallow copy of s.
Note, the non-operator versions of union(), intersection(), difference(), and symmetric_difference(), issubset(), and issuperset() methods will accept any iterable as an argument. In contrast, their operator based counterparts require their arguments to be sets. This precludes error-prone constructions like set(‘abc’) & ‘cbs’ in favor of the more readable set(‘abc’).intersection(‘cbs’).
Both set and frozenset support set to set comparisons. Two sets are equal if and only if every element of each set is contained in the other (each is a subset of the other). A set is less than another set if and only if the first set is a proper subset of the second set (is a subset, but is not equal). A set is greater than another set if and only if the first set is a proper superset of the second set (is a superset, but is not equal).
Instances of set are compared to instances of frozenset based on their members. For example, set(‘abc’) == frozenset(‘abc’) returns True and so does set(‘abc’) in set([frozenset(‘abc’)]).
The subset and equality comparisons do not generalize to a total ordering function. For example, any two nonempty disjoint sets are not equal and are not subsets of each other, so all of the following return False: a
相关文章推荐
- python 内置数据结构的基本操作 —— Set(1)
- python 内置数据结构的基本操作 —— list(2)
- week03_python内置数据结构_set及操作
- python 内置数据结构的基本操作 —— list(2)
- python 内置数据结构的基本操作 —— dict(1)
- python 内置数据结构的基本操作 —— dict(2)
- python 内置数据结构的基本操作 —— tuple(2)
- python中set(可变集合)、frozenset(不可变集合)的基本操作
- python数据结构之列表基本操作[学习笔记]
- python之set集合基本操作
- Python内置数据结构——集合set ,集合运算
- Python实现基本数据结构中栈的操作示例
- python set基本操作
- Python实现基本数据结构---栈操作
- 5.Python基本数据结构-字典-创建/访问/基本操作/格式化输出
- python数据结构之链表——基本操作实现
- python内置数据结构之set
- python 内置数据结构的基本操作 —— tuple(1)
- python数据结构-序列\序列通用操作\列表(基本操作、方法)
- Python实现基本数据结构---队列操作