Therera re a number of set
comparisons. All
of the standard comparisons (<
, <=
,
>
, >=
, ==
, !=
,
in
,
not in
) work with
set
s, but the interpretation of the operators is
based on set
theory. For example, the comparisons
determine if we have subset or superset (<=
,
>=
) relationships between two
set
s.
The basic in and not in operators are the basic membership tests.
For example, the set
craps
is
all of the ways we can roll craps on a come out roll. We've modeled a
throw of the dice as a 2-tuple
. We can now test a
specific throw to see if it is craps.
>>>
craps= set( [ (1,1), (2,1), (1,2), (6,6) ] )
>>>
(1,2) in craps
True
>>>
(3,4) in craps
False
The ordering operators (<
, <=
,
>
, >=
) comoare two
set
s to determine their superset or subset
relationship. These operators reflect the two definitions of subset (and
superset). S1 is a subset of S2 if every element of S1 is in S2. The
basic subset test is the <=
operator; it says nothing
about "extra" elements in S2. S1 is a proper subset of
S2 if every element of S1 is in S2 and S2 has at least one additional
element, not in S1. The proper subset test is the <
operator.
Here we've defined three
to hold both of the
dice rolls that total 3. When we compare three
with
craps
, we see the expected relationships:
three
is a subset craps
as well as
a proper subset of craps
.
>>>
three= set( [ (2,1), (1,2) ] )
>>>
three < craps
True
>>>
three <= craps
True
>>>
craps <= craps
True
>>>
craps < craps
False
We can also see that any given set
is a
subset of itself, but is never a proper subset of itself.