-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathset.py
More file actions
158 lines (120 loc) · 4.59 KB
/
Copy pathset.py
File metadata and controls
158 lines (120 loc) · 4.59 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# a set is an unordered collection of unique elements
a = {1,2,3}
print('set a', a)
# check type
print('type of a', type(a))
# mixed elements
a = {1,2,True,"Hello World"}
print('mixed set a', a)
# a set will filter duplicate elements
a = {1,2,3,4,4,5}
print('duplicate elements set', a)
# a set, unlike tuple, must not contain mutable elements
# a = {1,2,[3,4]} # TypeError: unhashable type: 'list' ==> https://stackoverflow.com/questions/14535730/what-does-hashable-mean-in-python
# a = {1,2,(1,2,[3,4])} # error if tuple contains nested mutable elements
# a = {1,2,{3,3,4}} # error as a set is mutable
# create set from a iterable
a = set([1,2,3,3,4])
print('set([1,2,3,3,4])', a)
# empty set
print('type(set()), type({}) => ', type(set()), type({}))
# since set is unordered, we can not access the elements
a = {1,2,3}
# b = a[0] #TypeError: 'set' object is not subscriptable
# to add new element, use add method
a = {1,2,3}
a.add(4)
print('after add() a =>', a)
# set is mutable, hence use copy() to clone a set
a = {1,2,3}
b = a
c = a.copy()
b.add(4)
c.add(5)
print('set a after add operations on b & c', a)
# to add elements from a iterable, use update method
a = {1,2,3}
a.update([4,5],(6,7,7),{8,9},"GAMMA") # single level only
print('after update() =>', a)
# merge two dictionaries using + operator does not work
a = {1,2,3}
b = {3,4,5}
# c = a + b # TypeError: unsupported operand type(s) for +: 'set' and 'set'
# remove element from a set
a = {1,2,3,4,5}
a.discard(1) # one argument, non iterable
# a.remove(20) # same as discard but raise value KeyError if element doesn't exist
print('after removal a =>', a)
# pop an element
# for empty set, raises KeyError
a = {1,2,3,4,5}
b = a.pop() # remove arbitary element as set is unordered
print(a, b)
# empty a set
a = {1,2,3,4,5}
a.clear()
print('after clear() a =>', a)
#######################################
# set operations #
a = {4,5,6,7,8,9}
b = {1,20,3,40,5,6,70,8,90}
# union of the set (all elements combined without duplicates)
union = a | b # same as a.union(b) or b.union(a)
print('union', union)
# intersection of set (all common elements)
# a.intersection_update(b): to update set a with results
intersection = a & b # same as a.intersection(b) and b.intersection(a)
print('intersection', intersection)
# difference: A - B yields all elements of A without matching elements of B
# a.difference_update(b) :to update set a with results
difference_a_min_b = a - b # a.difference(b) # elements of a, which are not in b
difference_b_min_a = b - a # b.difference(a) # elements of b, which are not in a
print('difference_a_min_b', difference_a_min_b)
print('difference_b_min_a', difference_b_min_a)
# symmetric difference (common elements)
# a.symmetric_difference_update(b) : to update set a with results
sym_diff = a ^ b # same as a.symmetric_diiference(b) or b.symmetic_difference(a)
print('sym_diff', sym_diff)
# isdisjoint: two sets are disjoint if they have no common elements
a = {1,2,3}
b = {4,5,6}
a_b_is_disjoint = a.isdisjoint(b) # same as b.disjoint(a)
print('a_b_is_disjoint', a_b_is_disjoint)
# issubset: if elements of one set exits in another
a = {1,2,3}
b = {1,2,3,4,5,6,7,8,9}
is_a_subset_of_b = a.issubset(b) # yes, all elements of a in b
is_b_subset_of_a = b.issubset(a) # no, all elements of b does not comtain in a
print('is_a_subset_of_b', is_a_subset_of_b)
print('is_b_subset_of_a', is_b_subset_of_a)
# superset: when one set contains elements of another
a = {1,2,3}
b = {1,2,3,4,5,6,7,8,9}
is_a_superset_of_b = a.issuperset(b) # no, as a does not contain all elements of b
is_b_superset_of_a = b.issuperset(a) # yes, as b contains all elements of a
print('is_a_superset_of_b', is_a_superset_of_b)
print('is_b_superset_of_a', is_b_superset_of_a)
#####################################################
# python set implements iterable interface
a = {1,2,3,4,5}
print("for loop on set a =>", end=" ")
for val in a:
print(val, end=" ")
print("")
# check if item exists in a set
a = {1,2,3}
print('2 in a', 2 in a)
print('4 not in a', 4 not in a)
# set comprehension
a = {x for x in range(10) if x % 2 == 0}
print('a from set comprehension', a)
#####################################################
# a fronzenset is immutable set. Like tuple is immutable list, a frozenset is immutable set.
# but a frozenset provides API to interate theough its elements
a = frozenset([1,2,4,3,4,1]) # from a list
b = frozenset({5,66,5,8,66}) # from a set # any iterable works
print('frozenset a and b', a, b)
# methods
# all methods except add or update as they will try to change the set
# other built-in important methods
# https://www.programiz.com/python-programming/methods/set