-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_sets.py
More file actions
61 lines (56 loc) · 1.58 KB
/
Copy pathmy_sets.py
File metadata and controls
61 lines (56 loc) · 1.58 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
"""
Learn about sets
An unordered collection of unique, immutable objects
Define it using { }
You can use the set() constructor to create one
"""
def main():
"""
Test function
:return:
"""
p = {6, 78, 21, 45}
print(p, type(p))
data = [1, 3, 5, 2, 88, 3, 1]
print(data, type(data))
# eliminate duplicates
sdata = set(data)
print(sdata, type(sdata))
# Iterate with for
for item in sdata:
print(item)
# Supports membership testing: in, not in
print(5 in sdata)
# Adding elements to sets:
sdata.add(45)
print(sdata)
sdata.update([2, 99, 44, 33, 1, 2, 88])
print(sdata)
# Removing elements
# remove() method: raises KeyError if not found
sdata.remove(44)
print(sdata)
# discard() method: does not raises any Error
sdata.discard(77)
print(sdata)
# Copying sets
bk_data = sdata.copy()
print(bk_data is sdata)
print(bk_data == sdata)
########## Define some sets of data
blue_eyes = {"Olivia", "Harry", "Lily", "Jack"}
blond_hair = {"Harry", "Jack", "Amelia", "Mia", "Joshua"}
smell_hcn = {"Harry", "Amelia"}
taste_ptc = {"Harry", "Lily", "Amelia", "Lola"}
o_blood = {"Mia", "Joshua", "Lily", "Olivia"}
b_blood = {"Amelia", "Jack"}
a_blood = {"Harry"}
ab_blood = {"Joshua", "Lola"}
print(blue_eyes.union(blond_hair))
print(blue_eyes.intersection(taste_ptc))
print(smell_hcn.symmetric_difference(a_blood))
print(blond_hair.difference(ab_blood))
print(taste_ptc.issuperset(smell_hcn))
if __name__ == '__main__':
main()
exit(0)