Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex


5.7.1 Set Objects

Instances ofSet andImmutableSet both providethe following operations:

OperationEquivalentResult
len(s)cardinality of sets
x instestx for membership ins
x not instestx for non-membership ins
s.issubset(t)s <=ttest whether every element ins is int
s.issuperset(t)s >=ttest whether every element int is ins
s.union(t)s |tnew set with elements from boths andt
s.intersection(t)s &tnew set with elements common tos andt
s.difference(t)s -tnew set with elements ins but not int
s.symmetric_difference(t)s ^tnew set with elements in eithers ort but not both
s.copy()new set with a shallow copy ofs

Note, the non-operator versions ofunion(),intersection(),difference(), andsymmetric_difference() will accept any iterable as an argument.In contrast, their operator based counterparts require their arguments tobe sets. This precludes error-prone constructions likeSet('abc') & 'cbs' in favor of the more readableSet('abc').intersection('cbs').Changed in version 2.3.1:Formerly all arguments were required to be sets.

In addition, bothSet andImmutableSetsupport set to set comparisons. Two sets are equal if and only ifevery element of each set is contained in the other (each is a subsetof the other).A set is less than another set if and only if the first set is a propersubset 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 propersuperset of the second set (is a superset, but is not equal).

The subset and equality comparisons do not generalize to a completeordering function. For example, any two disjoint sets are not equal andare not subsets of each other, soall of the following returnFalse:a<b,a==b, ora>b.Accordingly, sets do not implement the__cmp__ method.

Since sets only define partial ordering (subset relationships), the outputof thelist.sort() method is undefined for lists of sets.

The following table lists operations available inImmutableSetbut not found inSet:

OperationResult
hash(s)returns a hash value fors

The following table lists operations available inSetbut not found inImmutableSet:

OperationEquivalentResult
s.update(t)s |=treturn sets with elements added fromt
s.intersection_update(t)s &=treturn sets keeping only elements also found int
s.difference_update(t)s -=treturn sets after removing elements found int
s.symmetric_difference_update(t)s ^=treturn sets with elements froms ort but not both
s.add(x)add elementx to sets
s.remove(x)removex from sets; raisesKeyError if not present
s.discard(x)removesx from sets if present
s.pop()remove and return an arbitrary element froms; raisesKeyError if empty
s.clear()remove all elements from sets

Note, the non-operator versions ofupdate(),intersection_update(),difference_update(), andsymmetric_difference_update() will accept any iterable asan argument.Changed in version 2.3.1:Formerly all arguments were required to be sets.

Also note, the module also includes aunion_update() methodwhich is an alias forupdate(). The method is included forbackwards compatibility. Programmers should prefer theupdate() method because it is supported by the builtinset() andfrozenset() types.


Up one LevelPython Library ReferenceContentsModule IndexIndex

Release 2.5.2, documentation updated on 21st February, 2008.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2025 Movatter.jp