Comparing two sets in python
Question:
Why do I get the results shown?
>>> x = {"a","b","1","2","3"}
>>> y = {"c","d","f","2","3","4"}
>>> z=x<y
>>> print(z)
False
>>> z=x>y
>>> print(z)
False
Answers:
The <
and >
operators are testing for strict subsets. Neither of those sets is a subset of the other.
{1, 2} < {1, 2, 3} # True
{1, 2} < {1, 3} # False
{1, 2} < {1, 2} # False -- not a *strict* subset
{1, 2} <= {1, 2} # True -- is a subset
When working with sets, > and < are relational operators.
hence, these operations are used to see if one set is the proper subset of the other, which is False for as neither is the proper subset of the other.
Straight from the python documentation —
In addition, both Set and ImmutableSet 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).
Why do I get the results shown?
>>> x = {"a","b","1","2","3"}
>>> y = {"c","d","f","2","3","4"}
>>> z=x<y
>>> print(z)
False
>>> z=x>y
>>> print(z)
False
The <
and >
operators are testing for strict subsets. Neither of those sets is a subset of the other.
{1, 2} < {1, 2, 3} # True
{1, 2} < {1, 3} # False
{1, 2} < {1, 2} # False -- not a *strict* subset
{1, 2} <= {1, 2} # True -- is a subset
When working with sets, > and < are relational operators.
hence, these operations are used to see if one set is the proper subset of the other, which is False for as neither is the proper subset of the other.
Straight from the python documentation —
In addition, both Set and ImmutableSet 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).