1 * Dictionaries |
|
2 *** Outline |
|
3 ***** Dictionaries |
|
4 ***** Sets |
|
5 ***** Arsenal Required |
|
6 *** Script |
|
7 Welcome friends. |
|
8 |
|
9 In previous tutorial we covered Lists, Tuples and related |
|
10 functions. In this session we shall continue with Python |
|
11 data structures and cover Dictionaries and sets. We have already |
|
12 covered some basics of Dictionaries in session on Statistics. Here |
|
13 we shall revisit those concepts and some new ones. |
|
14 |
|
15 We give it a name and it returns a corresponding number. |
|
16 Dictionaries are just key-value pair. For each 'key' there is |
|
17 corresponding 'value' associated with it. In lists we use indexes |
|
18 to access elements, here we use the 'key'. |
|
19 |
|
20 Lets start by opening IPython interpreter. |
|
21 '{}' are used to create Python dictionaries. Lets create a dictionary say |
|
22 |
|
23 player = {'Mat': 134,'Inn': 233, |
|
24 'Runs': 10823, 'Avg': 52.53} |
|
25 Let's see what player contains by typing: |
|
26 |
|
27 print player |
|
28 |
|
29 Its a dictionary storing statistics of a cricket player. |
|
30 Here 'Mat', 'Inn' etc are the keys. Now in order to get the 'average' of |
|
31 this player we simply type |
|
32 print player['Avg'] |
|
33 52.53 |
|
34 |
|
35 To add a new key-value pair to this dictionary we type |
|
36 player['Name'] = 'Rahul Dravid' |
|
37 print player |
|
38 As you can see the given key-value pair has been added. |
|
39 Please note that Python dictionaries don't maintain the order |
|
40 in which the key-value pairs are stored. The order might change |
|
41 as we add new entries. |
|
42 |
|
43 In dictionaries Duplicate keys are overwritten, that is when we do |
|
44 player['Mat'] = 139 |
|
45 It wont create a new entry, rather it will simply overwrite previous |
|
46 value with the new one. So |
|
47 print player |
|
48 will have updated value |
|
49 |
|
50 As we covered in one of previous sessions 'for' can be used to iterate |
|
51 through lists. The same is possible in case of dictionaries too. We can |
|
52 iterate over them using the 'keys', for example: |
|
53 for key in player: |
|
54 print key, player[key] |
|
55 This prints the keys in the dictionary along with their corresponding |
|
56 values. Notice that the order is not the same as we entered it. |
|
57 |
|
58 We saw how containership works in lists. There we can check if a |
|
59 value is present in a list or not but in case of Dictionaries we |
|
60 can only check for the containership of the keys. so |
|
61 'Inn' in player |
|
62 returns True |
|
63 'Econ' in Player |
|
64 returns False as there is no such 'key' |
|
65 If you try to look or search for a 'value' it will not work. |
|
66 Dictionaries support functions to retrieve keys and values |
|
67 such as |
|
68 player.keys() |
|
69 returns the list of all 'keys' |
|
70 player.values() |
|
71 return list of all 'values' |
|
72 |
|
73 Now we shall move on to 'sets'. Sets in Python are an unordered |
|
74 collection of unique elements. This data structure comes in handy in |
|
75 situations while removing duplicates from a sequence, and computing |
|
76 standard math operations on sets such as intersection, union, |
|
77 difference, and symmetric difference. |
|
78 |
|
79 Lets start by creating a set |
|
80 f10 = set([1,2,3,5,8]) |
|
81 And thats how a set is created. |
|
82 f10 is the set of Fibonacci numbers less than 10 |
|
83 lets print the value of f10 |
|
84 print f10 |
|
85 |
|
86 As we mentioned earlier, these are unordered structure so order of |
|
87 elements is not maintained, and output order is different than |
|
88 input order, just as in dictionaries. Lets create one more set, a set of |
|
89 all prime numbers less than 10 |
|
90 p10 = set([2,3,5,7]) |
|
91 print p10. |
|
92 |
|
93 To get union of these two sets we use the or '|' operator |
|
94 f10 | p10 |
|
95 |
|
96 For intersection we use the and '&' operator: |
|
97 f10 & p10 |
|
98 |
|
99 f10 - p10 gives difference between f10 and p10, that is, the set of all elements |
|
100 present in f10 but not in p10. |
|
101 The carat '^' operator gives us the symmetric difference of 2 sets. That is |
|
102 f10 union p10 minus f10 intersection p10 |
|
103 f10 ^ p10 |
|
104 |
|
105 To check if a set is the super set or a subset of another set, the greater than |
|
106 and the lesser than operators are used |
|
107 set([2,3]) < p10 |
|
108 returns True as p10 is superset of given set |
|
109 |
|
110 Similar to lists and dictionaries, sets also supports containership so |
|
111 2 in p10 |
|
112 returns True as 2 is part of set p10 and |
|
113 4 in p10 |
|
114 returns False. |
|
115 |
|
116 The 'len' function works with sets also: |
|
117 len(f10) returns the length, which is 5 in this case. |
|
118 We can also use 'for' loops to iterate through a set just as with dictionaries and lists. |
|
119 |
|
120 With this we come to the end of this tutorial on Dictionaries and |
|
121 sets. We have seen how to initialize dictionaries, how to index them using keys |
|
122 and a few functions supported by dictionaries. We then saw how to initialize |
|
123 sets, perform various set operations and a few functions supported |
|
124 by sets. Hope you have enjoyed it, Thank you. |
|
125 |
|
126 *** Notes |
|