7. Set¶
Type of data - same as in mathematics
7.1. Defining¶
>>> A = {1, 2, 3, 4, 5}
>>> B = {4, 5, 6, 7, 8}
7.2. Checking type¶
>>> type(A)
<class 'set'>
7.3. Operation on sets¶
Checking set length
>>> len(A)
5
Checking element occurrence
>>> 17 in A
False
>>> 3 in A
True
Adding element to the set
>>> A.add(17)
>>> 17 in A
True
Union
C = A | B
print(C)
{1, 2, 3, 4, 5, 6, 7, 8, 17}
>>> print(C.issuperset(A))
True
>>> print(C.issuperset(A))
True
Intersection - Common set
>>> D = A & B
>>> print(D)
{4, 5}
Difference
E = A - B
print(E)
{1, 2, 3, 17}
F = B - A
print(F)
{8, 6, 7}
Symmetric difference
>>> print(A.symmetric_difference(B))
{1, 2, 3, 17, 6, 7, 8}
7.4. Immutable sets¶
>>> A = frozenset([1, 2, 3, 4])
7.6. Exercises¶
7.6.1. Pierwsze¶
mając biory:
A = {‘wp.pl’, ‘onet.pl’, ‘google.com’, ‘ing.pl’, ‘facebook.com’}
B = {‘wp.pl’, ‘youtube.pl’, ‘wikipedia.org’, ‘ovh.com’, ‘facebook.com’}
Znajdz:
Te domeny które w obu zbirach są wspolne
Domeny występującę wyłącznie albo w jednym albo w drugim zbiorze
7.6.2. Drugie¶
mając listę [1, 2, 4, 5, 7, 7, 7] wyświetl jedynie jej unikatowe wartości