These operations are supported by both set
and
frozenset
types:
x
in S
Predicate that tests
whether element
is a member of set x
.
S
>>> 1 in set([0,1,4]) True >>> 99 in set([0,1,4]) False
x
not in S
Predicate that tests
whether element
is not a member of set
x
.
S
>>> 1 not in set([0,1,4]) False >>> 99 not in set([0,1,4]) True
S1
== S2
Predicate that tests
whether sets
and S1
have exactly the same members.
S2
>>> set('bedac') == set('abcde') True >>> set('bedac') == set('bedack') False
S1
!= S2
Predicate that tests
whether sets
and S1
have different members.
S2
>>> set ( 'bedac' ) != set ( 'abcde' ) False >>> set('bedac')!=set('bedack') True
S1
< S2
Predicate that tests
whether
is a proper subset of S1
; that is, all the elements of
S2
are also
members of S1
, but there is at least one element of S2
that is not in
S2
.
S1
>>> set('ab') < set('ab') False >>> set('ab') < set('abcde') True
S1
> S2
Predicate that tests
whether
is a proper superset of S1
; that is, all the elements of
S2
are also
members of S2
, but there is at least one element of S1
that is not in
S1
.
S2
>>> set('ab') > set('ab') False >>> set('abcde') > set('cd') True
S
.copy()
Return a new set of the same type as
, containing all the same
elements.
S
>>> s1=set('aeiou') >>> s2=s1 >>> s3=s1.copy() >>> s1.add('y') >>> s1 set(['a', 'e', 'i', 'o', 'u', 'y']) >>> s2 set(['a', 'e', 'i', 'o', 'u', 'y']) >>> s3 set(['a', 'i', 'e', 'u', 'o'])
S1
.difference(S2
)
Returns a new set of the same type as
, containing only those values
found in S1
but not found in S1
. The
S2
argument
may be a set or a sequence.
S2
>>> set('roygbiv').difference('rgb') set(['i', 'o', 'v', 'y'])
S1
- S2
Same as
,
except that S1
.difference(S2
)
must be a set.
S2
>>> set('roygbiv') - set('rgb') set(['i', 'y', 'o', 'v'])
S1
.intersection(S2
)
Returns a new set, of the same type as
, containing only the elements
found both in S1
and S1
.
S2
may be a
set or a sequence.
S2
>>> set([1,2,3,5,7,11]).intersection(set([1,3,5,7,9])) set([1, 3, 5, 7]) >>> set([1,3,5]).intersection( (2,4,6,8) ) set([])
S1
& S2
Same as
,
but S1
.intersection(S2
)
must be
a set.
S2
S1
.issubset(S2
)
Predicate that tests
whether every element of
is also in S1
. S2
may be a set or a sequence.
S2
>>> set([1,2]).issubset(set([2,4,1,8])) True >>> set([2,4,1,8]).issubset(set([1,2])) False >>> set(['r', 'g', 'b']) <= set(['r', 'o', 'y', 'g', 'b', 'i', 'v']) True
S1
<= S2
Same as
, but
S1
.issubset(S2
)
must be a
set.
S2
S1
.issuperset(S2
)
Predicate that tests
whether every element of
is also in S2
. S1
may be a set or a sequence.
S2
>>> set([1,2]).issuperset(set([2,4,1,8])) False >>> set([2,4,1,8]).issuperset(set([1,2])) True
S1
>= S2
Same as
.
S1
.issuperset(S2
)
S1
.symmetric_difference(S2
)
Returns a new set of the same type as S1
, containing only elements found in
or S1
, but not found in
both. The S2
argument may be a set or a sequence.
S2
>>> set('aeiou').symmetric_difference('etaoin') set(['n', 'u', 't'])
S1
^ S2
Same as
, but S1
.symmetric_difference(S2
)
must be a set.
S2
S1
.union(S2
)
Returns a new set, with the same type as
, containing all
the elements found in either S1
or S1
.
S2
The
argument may be a set or a sequence.
S2
>>> set([1,2]).union(set([1,3,7])) set([1, 2, 3, 7]) >>> set([1,2]).union( (8,2,4,5) ) set([8, 1, 2, 4, 5])
S1
| S2
Same as
.
S1
.union(S2
)