Validating set¶
Here are some of the operations that can be performed to validate sets using Python as programming language.* Checking if an element exists (using in operator).
issubset
– checking if first set is subset of second setissuperset
– checking if first set is superset of second setisdisjoint
– check if 2 sets have common elements
In [1]:
s = {1, 2, 3, 3, 4, 4, 4, 5}
In [2]:
1 in s
Out[2]:
True
In [3]:
s1 = {1, 2, 3}
In [4]:
s2 = {1, 2, 3, 4, 5}
In [5]:
s1.issubset?
Docstring: Report whether another set contains this set. Type: builtin_function_or_method
In [6]:
s1.issubset(s2)
Out[6]:
True
In [7]:
s1.issuperset(s2)
Out[7]:
False
In [8]:
s2.issuperset(s1)
Out[8]:
True
In [9]:
s1.issuperset(s2)
Out[9]:
False
In [10]:
s1.isdisjoint?
Docstring: Return True if two sets have a null intersection. Type: builtin_function_or_method
In [11]:
s1 = {1, 2, 3, 4}
In [12]:
s2 = {3, 4, 5, 6, 7}
In [13]:
s1.isdisjoint(s2)
Out[13]:
False
In [14]:
s1 = {1, 2, 3, 4}
In [15]:
s2 = {5, 6, 7}
In [16]:
s1.isdisjoint(s2)
Out[16]:
True