forked from shubhansu-kr/INT213-Python-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15_Sets.py
145 lines (101 loc) · 2.57 KB
/
15_Sets.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
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
# Sets : Store data/collection
# Sets are unordered in nature, we cannot use indexing
# No duplicacy of data: Only once occurence of one element
# Even if i add element more than once, element is only inserted once
a = {1, 5, 2, 2, 4}
print (a)
# <class 'set'>
print(type(a))
# Type casting
x = [1,3,2,5,3,5,4,6,7, 8]
y = (4, 2, 1, 5, 6, 3, 21)
w = set(x)
print(w)
z = set(y)
print(y)
# Dictionary and set have same curly braces
# so to create an empty set, we have to type cast
a = {}
b = set({})
print (type(a)) # <class 'dict'>
print (type(b)) # <class 'set'>
# print (z[0]) Error: Cannot access the set element
# We can though iterate using loops
for i in z:
print(i)
# Methods in loops:
print(z)
# add
z.add(3) # add already existing element
print(z) # No change in set
z.add(11) # add a new element
print (z) # new element inserted
# remove
# z.remove(32) # Remove a non-existent element: Cannot remove (error)
print (z) # No change in set
z.remove(3) # Removes the 3
print(z)
# discard
z.discard(32) # No error even if the element does not exist
print(z) # Remove only if element is present
z.discard(11)
print (z) #Removes the element 11
# pop : Takes no argument: Remove any random element
z.pop()
print(z)
# issubset : Check if z is a subset of w and return a boolean result
isSubset = z.issubset(w)
print (isSubset)
# difference : Returns a subset which contains the difference of two set
# elements which are present in only one set
# Order matters: Element which are present is q but not in z
q = w.difference(z)
print (q)
# Removes the common element of w and z
q = w.difference_update(z)
print(q)
# intersection : Returns the intersection of two sets
q = w.intersection(z)
print(q)
# union
q = w.union(z)
print (q)
# issuperset : Returns boolean for if the set is a superset
print(q.issuperset(w)) # q is a super set of w : true
print(w.issuperset(q)) # w is a super set of q : false
# isdisjoint : Check if both the sets are completely different
print (q.isdisjoint(w))
# Functions in set
# len
print(len(q))
# sorted : Return a list of sorted element: Not a set: return a list
print (q)
m = sorted(q)
print (m)
# max
print(max(q))
# min
print (min(q))
# sum
print (sum(q))
# enumerate
m = list(enumerate(q))
print (m)
# operations in sets
a = {1,2,3,4,5}
b = {4,5,6,7,8}
# union |
c = a | b
print (c)
# intersection &
c = a & b
print (c)
# difference -
c = a-b
print (c)
c = b-a
print(c)
# to check subset => <=
c = a|b
# symmertric difference ^ : ((Only in a) union (only in b))
# to check the existence of element in/not in