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