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