-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_methods.py
67 lines (49 loc) · 1.45 KB
/
set_methods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
s1 = {1, 2, 5, 6}
s2 = {3, 6, 7}
print(s1.union(s2))
print(s1, s2)
s1.update(s2)
print(s1, s2)
print(s1.intersection(s2))
# Symmetric Difference = (A union B) - (A intersection B)
# prints values which are not similar to both the sets.
print(s1.symmetric_difference(s2))
# difference() and difference_update()
s1 = {1, 2, 5, 6}
s2 = {3, 6, 7, 9}
print(s1.difference(s2)) # returns a new set, without the unwanted items
s1.difference_update(s2)
print("Difference: ",s1) # method removes the items that exist in both sets.
A = {'a', 'c', 'g', 'd'}
B = {'c', 'f', 'g'}
print('A before (A - B) =', A)
A.difference_update(B)
print('A after (A - B) = ', A)
# Checks if items in a given set is present in the other set. Returns false if items are present else, return True.
s1 = {1, 2, 5, 6}
s2 = {3, 6, 7, 9}
print(s1.isdisjoint(s2))
# Checks if all the items of a particular set are present in the original set. Returns True if all the items are present, else it returns False.
s1 = {1, 2, 5, 6}
s2 = {1, 2, 5, 3, 6, 7, 9}
print(s2.issuperset(s1))
s1.add(45)
print(s1)
# remove or discard an element from the set
s1.remove(45) # gives error if not found and the rest of the lines would not work. Used when you want to throw error.
print(s1)
s1.discard(6)
print(s1)
random = s1.pop()
print(random)
del s1
print("s1 deleted")
# Clears set not deletes
s1 = {1, 2, 5, 6}
s2 = {1, 2, 5, 3, 6, 7, 9}
s1.clear()
print(s1)
if 2 in s2:
print("Present!")
else:
print("Absent!")