-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsets-join.py
More file actions
96 lines (58 loc) · 1.7 KB
/
sets-join.py
File metadata and controls
96 lines (58 loc) · 1.7 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'''
Join Sets
There are several ways to join two or more sets in Python.
The union() and update() methods joins all items from both sets.
The intersection() method keeps ONLY the duplicates.
The difference() method keeps the items from the first set that are not in the other set(s).
The symmetric_difference() method keeps all items EXCEPT the duplicates.
'''
# Union
set1 = {"a", "b", "c"}
set2 = {1, 2, 3}
set3 = {"John", "Elena"}
set4 = {"apple", "bananas", "cherry"}
set5 = set1.union(set2)
print(set5)
# Pipe |
set6 = set1 | set2
print(set6)
# multiple sets
myset = set1.union(set2, set3, set4)
myset1 = set1 | set2 | set3 | set4
print(f"This is union join {myset}")
print(f"this is | sign join {myset1}")
# Join Sets and tuple
x = {"a", "b", "c"}
y = (1, 2, 3)
z = x.union(y)
print(z)
# Update
set1.update(set2)
print(set1)
# Intersection
set10 = {"apple", "banana", "cherry"}
set20 = {"google", "microsoft", "apple"}
set30 = set10 & set20
set40 = set10.intersection(set20)
set10.intersection_update(set2)
print(set40)
print(set30)
print(set10)
print("----------")
set11 = {"apple", "banana", "cherry"}
set22 = {"google", "microsoft", "apple"}
# Difference
set33 = set11.difference(set22)
print(set33)
set34 = set11 - set22
print(set34)
set11.difference_update(set22)
print(set11)
# Symmetric Differences : The symmetric_difference() method will keep only the elements that are NOT present in both sets.
set35 = set11.symmetric_difference(set22)
print(set35)
set36 = set11 ^ set22
print(set36)
# The symmetric_difference_update() method will also keep all but the duplicates, but it will change the original set instead of returning a new set.
set11.symmetric_difference_update(set22)
print(set11)