English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Méthodes de collection en Python
If two sets are disjoint sets, the isdisjoint() method returns True. If not, it returns False.
If two sets have no common elements, they are called disjoint sets. For example:
A = {1, 5, 9, 0} B = {2, 4, -5}
In this case, set A and B are disjoint sets.
The syntax of isdisjoint() is:
set_a.isdisjoint(set_b)
The isdisjoint() method takes a single parameter (a group).
You can also pass an iterable (list, tuple, dictionary, and string) to isdisjoint(). The isdisjoint() method will automatically convert the iterable object to a set and check if these sets are disjoint.
The isdisjoint() method returns
True if the two sets are disjoint sets (in the above syntax if set_a and set_b are disjoint sets)
False if the two sets are not disjoint sets
A = {1, 2, 3, 4} B = {5, 6, 7} C = {4, 5, 6} print('A and B are disjoint?', A.isdisjoint(B)) print('A and C are disjoint?', A.isdisjoint(C))
When running this program, the output is:
Are A and B disjoint? True Are A and C disjoint? False
A = {'a', 'b', 'c', 'd'} B = ['b', 'e', 'f'] C = '5de4' D = {1 : 'a', 2 : 'b'} E = {'a' : 1, 'b' : 2} print('A and B are disjoint?', A.isdisjoint(B)) print('A and C are disjoint?', A.isdisjoint(C)) print('A and D are disjoint?', A.isdisjoint(D)) print('A and E are disjoint?', A.isdisjoint(E))
When running this program, the output is:
Are A and B disjoint? False Are A and C disjoint? False Are A and D disjoint? True Are A and E disjoint? False