The complement of a set is everything not in the set, but part of the 'universal set'. Without a definition of the universal set, you can't really give a standard-library definition of the complement of a set.
Moreover, the Python set
type deals in sets of discrete objects, not a mathematical construct that could be infinitely large, such as all natural numbers. So Python doesn't support the general, nebulous and infinite idea of a
single universal set.
For specific domains, if you can define the universal set in discrete terms, simply define your own complement[]
callable. For example, given a global definition of the universal set U, you can define the complement of a set a
as the difference between U and that set:
U = {'pink', 'purple', 'red', 'blue', 'gray', 'orange', 'green', 'yellow', 'indigo', 'violet'}
def complement[a]:
# difference between a global universal set U and the given set a
return U - a
or, simpler still:
complement = U.difference
Then just pass a set to complement[]
to test the hypothesis:
>>> # set | set == set union, set & set == set intersection
...
>>> complement[B | C] == complement[B] & complement[C]
True
So yes, your interpretation is correct, U - B
produces the
complement of B
.
Introduction
A set is an unordered collection of unique elements. It is one of the most core data structures in computer science. Like other programming languages, Python has built-in implementations of the set and its operation functions.
Since the features and manipulations of sets are based on the set theory, it’s different from other data structures and may be confusing…
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
The difference between the two sets in Python is equal to the difference between the number of elements in two sets. The function difference[]
returns a set that is the difference between two sets. Let’s try to find out what will be the difference between two sets A and B. Then [set A – set B] will be the elements present in set A but not in B and [set B – set A] will be the elements present in set B but not in set A.
Example:
set A = {10, 20, 30, 40, 80} set B = {100, 30, 80, 40, 60} set A - set B = {10, 20} set B - set A = {100, 60} Explanation: A - B is equal to the elements present in A but not in B B - A is equal to the elements present in B but not in A
Let’s look at the Venn diagram of the following difference set function.
Syntax:
set_A.difference[set_B] for [A - B] set _B.difference[set_A] for [B - A]
In this program, we will try to find out the difference between two sets set_A and set_B, both the way:
A
=
{
10
,
20
,
30
,
40
,
80
}
B
=
{
100
,
30
,
80
,
40
,
60
}
print
[A.difference[B]]
print
[B.difference[A]]
Output:
{10, 20} {100, 60}
We can also use – operator to find the difference between two sets.
A
=
{
10
,
20
,
30
,
40
,
80
}
B
=
{
100
,
30
,
80
,
40
,
60
}
print
[A
-
B]
print
[B
-
A]
Output:
{10, 20} {100, 60}
If we have equal sets then it will return the null set.
A
=
{
10
,
20
,
30
,
40
,
80
}
B
=
{
10
,
20
,
30
,
40
,
80
,
100
}
print
[A
-
B]