1 Hello friends and welcome to the tutorial on Sets |
|
2 |
|
3 {{{ Show the slide containing title }}} |
|
4 |
|
5 {{{ Show the slide containing the outline slide }}} |
|
6 |
|
7 In this tutorial, we shall learn |
|
8 |
|
9 * sets |
|
10 * operations on sets |
|
11 |
|
12 Sets are data structures which contain unique elements. In other words, |
|
13 duplicates are not allowed in sets. |
|
14 |
|
15 Lets look at how to input sets. |
|
16 type |
|
17 :: |
|
18 |
|
19 a_list = [1, 2, 1, 4, 5, 6, 7] |
|
20 a = set(a_list) |
|
21 a |
|
22 |
|
23 We can see that duplicates are removed and the set contains only unique |
|
24 elements. |
|
25 :: |
|
26 |
|
27 f10 = set([1, 2, 3, 5, 8]) |
|
28 p10 = set([2, 3, 5, 7]) |
|
29 |
|
30 f10 is the set of fibonacci numbers from 1 to 10. |
|
31 p10 is the set of prime numbers from 1 to 10. |
|
32 |
|
33 Various operations that we do on sets are possible here also. |
|
34 The | character stands for union |
|
35 :: |
|
36 |
|
37 f10 | p10 |
|
38 |
|
39 gives us the union of f10 and p10 |
|
40 |
|
41 The & character stands for intersection. |
|
42 :: |
|
43 |
|
44 f10 & p10 |
|
45 |
|
46 gives the intersection |
|
47 |
|
48 similarly, |
|
49 :: |
|
50 |
|
51 f10 - p10 |
|
52 |
|
53 gives all the elements that are in f10 but not in p10 |
|
54 |
|
55 :: |
|
56 |
|
57 f10 ^ p10 |
|
58 |
|
59 is all the elements in f10 union p10 but not in f10 intersection p10. In |
|
60 mathematical terms, it gives the symmectric difference. |
|
61 |
|
62 Sets also support checking of subsets. |
|
63 :: |
|
64 |
|
65 b = set([1, 2]) |
|
66 b < f10 |
|
67 |
|
68 gives a True since b is a proper subset of f10. |
|
69 Similarly, |
|
70 :: |
|
71 |
|
72 f10 < f10 |
|
73 |
|
74 gives a False since f10 is not a proper subset. |
|
75 hence the right way to do would be |
|
76 :: |
|
77 |
|
78 f10 <= f10 |
|
79 |
|
80 and we get a True since every set is a subset of itself. |
|
81 |
|
82 Sets can be iterated upon just like lists and tuples. |
|
83 :: |
|
84 |
|
85 for i in f10: |
|
86 print i, |
|
87 |
|
88 prints the elements of f10. |
|
89 |
|
90 The length and containership check on sets is similar as in lists and tuples. |
|
91 :: |
|
92 |
|
93 len(f10) |
|
94 |
|
95 shows 5. And |
|
96 :: |
|
97 |
|
98 1 in f10 |
|
99 2 in f10 |
|
100 |
|
101 prints True and False respectively |
|
102 |
|
103 The order in which elements are organised in a set is not to be relied upon |
|
104 since sets do not support indexing. Hence, slicing and striding are not valid |
|
105 on sets. |
|
106 |
|
107 {{{ Pause here and try out the following exercises }}} |
|
108 |
|
109 %% 1 %% Given a list of marks, marks = [20, 23, 22, 23, 20, 21, 23] |
|
110 list all the duplicates |
|
111 |
|
112 {{{ continue from paused state }}} |
|
113 |
|
114 Duplicates marks are the marks left out when we remove each element of the |
|
115 list exactly one time. |
|
116 |
|
117 :: |
|
118 |
|
119 marks = [20, 23, 22, 23, 20, 21, 23] |
|
120 marks_set = set(marks) |
|
121 for mark in marks_set: |
|
122 marks.remove(mark) |
|
123 |
|
124 # we are now left with only duplicates in the list marks |
|
125 duplicates = set(marks) |
|
126 |
|
127 {{{ Show summary slide }}} |
|
128 |
|
129 This brings us to the end of the tutorial. |
|
130 we have learnt |
|
131 |
|
132 * How to make sets from lists |
|
133 * How to input sets |
|
134 * How to perform union, intersection and symmectric difference operations |
|
135 * How to check if a set is a subset of other |
|
136 * The various similarities with lists like length and containership |
|
137 |
|
138 {{{ Show the "sponsored by FOSSEE" slide }}} |
|
139 |
|
140 #[Nishanth]: Will add this line after all of us fix on one. |
|
141 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
142 |
|
143 Hope you have enjoyed and found it useful. |
|
144 Thankyou |
|
145 |
|
146 .. Author : Nishanth |
|
147 Internal Reviewer 1 : |
|
148 Internal Reviewer 2 : |
|
149 External Reviewer : |
|
150 |
|
151 |
|
152 Questions |
|
153 ========= |
|
154 |
|
155 1. If ``a = [1, 1, 2, 3, 3, 5, 5, 8]``. What is set(a) |
|
156 |
|
157 a. set([1, 1, 2, 3, 3, 5, 5, 8]) |
|
158 #. set([1, 2, 3, 5, 8]) |
|
159 #. set([1, 2, 3, 3, 5, 5]) |
|
160 #. Error |
|
161 |
|
162 Answer: set([1, 2, 3, 5, 8]) |
|
163 |
|
164 2. ``a = set([1, 3, 5])``. How do you find the length of a? |
|
165 |
|
166 Answer: len(a) |
|
167 |
|
168 3. ``a = set([1, 3, 5])``. What does a[2] produce? |
|
169 |
|
170 a. 1 |
|
171 #. 3 |
|
172 #. 5 |
|
173 #. Error |
|
174 |
|
175 Answer: Error |
|
176 |
|
177 4. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What |
|
178 is the value of ``odd | squares``? |
|
179 |
|
180 Answer: set([1, 3, 4, 5, 7, 9, 16]) |
|
181 |
|
182 5. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What |
|
183 is the value of ``odd - squares``? |
|
184 |
|
185 Answer: set([3, 5, 7]) |
|
186 |
|
187 6. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What |
|
188 is the value of ``odd ^ squares``? |
|
189 |
|
190 Answer: set([3, 4, 5, 7, 16]) |
|
191 |
|
192 7. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What |
|
193 does ``odd * squares`` give? |
|
194 |
|
195 a. set([1, 12, 45, 112, 9]) |
|
196 #. set([1, 3, 4, 5, 7, 9, 16]) |
|
197 #. set([]) |
|
198 #. Error |
|
199 |
|
200 Answer: Error |
|
201 |
|
202 8. ``a = set([1, 2, 3, 4])`` and ``b = set([5, 6, 7, 8])``. What is ``a + b`` |
|
203 |
|
204 a. set([1, 2, 3, 4, 5, 6, 7, 8]) |
|
205 #. set([6, 8, 10, 12]) |
|
206 #. set([5, 12, 21, 32]) |
|
207 #. Error |
|
208 |
|
209 9. ``a`` is a set. how do you check if if a varaible ``b`` exists in ``a``? |
|
210 |
|
211 Answer: b in a |
|
212 |
|
213 10. ``a`` and ``b`` are two sets. What is ``a ^ b == (a - b) | (b - a)``? |
|
214 |
|
215 a. True |
|
216 #. False |
|
217 |
|
218 Answer: False |
|
219 |
|
220 |
|
221 Problems |
|
222 ======== |
|
223 |
|
224 1. Given that mat_marks is a list of maths marks of a class. Find out the |
|
225 no.of duplicates marks in the list. |
|
226 |
|
227 Answer:: |
|
228 |
|
229 unique_marks = set(mat_marks) |
|
230 no_of_duplicates = len(mat_marks) - len(unique_marks) |
|
231 |
|
232 2. Given that mat_marks is a list of maths marks of a class. Find how many |
|
233 duplicates of each mark exist. |
|
234 |
|
235 Answer:: |
|
236 |
|
237 marks_set = set(mat_marks) |
|
238 for mark in marks_set: |
|
239 occurences = mat_marks.count(mark) |
|
240 print occurences - 1, "duplicates of", mark, "exist" |
|