forked from joaoventura/full-speed-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionaries.tex
More file actions
118 lines (88 loc) · 3.1 KB
/
dictionaries.tex
File metadata and controls
118 lines (88 loc) · 3.1 KB
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
\chapter{Dictionaries}\label{dictionaries}
In this chapter we will work with Python dictionaries. Dictionaries are data structures that indexes values by a given key (key-value pairs). The following example shows a dictionary that indexes students ages by name.
\begin{lstlisting}
ages = {
"Peter": 10,
"Isabel": 11,
"Anna": 9,
"Thomas": 10,
"Bob": 10,
"Joseph": 11,
"Maria": 12,
"Gabriel": 10,
}
>>> print(ages["Peter"])
10
\end{lstlisting}
It is possible to iterate over the contents of a dictionary using "items", like this:
\begin{lstlisting}
>>> for name, age in ages.items():
... print(name, age)
...
Peter 10
Isabel 11
Anna 9
Thomas 10
Bob 10
Joseph 11
Maria 12
Gabriel 10
\end{lstlisting}
However, keys don't need to be necessarily strings and integers but can be any objects:
\begin{lstlisting}
d = {
0: [0, 0, 0],
1: [1, 1, 1],
2: [2, 2, 2],
}
>>> d[2]
[2, 2, 2]
\end{lstlisting}
Even more, you can use other dictionaries as values:
\begin{lstlisting}
students = {
"Peter": {"age": 10, "address": "Lisbon"},
"Isabel": {"age": 11, "address": "Sesimbra"},
"Anna": {"age": 9, "address": "Lisbon"},
}
>>> students['Peter']
{'age': 10, 'address': 'Lisbon'}
>>> students['Peter']['address']
'Lisbon'
\end{lstlisting}
This is quite useful to structure hierarchical information.
\section{Exercises with dictionaries}
Use the Python documentation at \url{https://docs.python.org/3/library/stdtypes.html#mapping-types-dict} to solve the following exercises.
Take the following Python dictionary:
\begin{verbatim}
ages = {
"Peter": 10,
"Isabel": 11,
"Anna": 9,
"Thomas": 10,
"Bob": 10,
"Joseph": 11,
"Maria": 12,
"Gabriel": 10,
}
\end{verbatim}
\begin{enumerate}
\item How many students are in the dictionary? Search for the "len" function.
\item Implement a function that receives the "ages" dictionary as parameter and return the average age of the students. Traverse all items on the dictionary using the "items" method as above.
\item Implement a function that receives the "ages" dictionary as parameter and returns the name of the oldest student.
\item Implement a function that receives the "ages" dictionary and a number "n" and returns a new dict where each student is $n$ years older. For instance, \textit{new\_ages(ages, 10)} returns a copy of "ages" where each student is 10 years older.
\end{enumerate}
\section{Exercises with sub-dictionaries}
Take the following dictionary:
\begin{verbatim}
students = {
"Peter": {"age": 10, "address": "Lisbon"},
"Isabel": {"age": 11, "address": "Sesimbra"},
"Anna": {"age": 9, "address": "Lisbon"},
}
\end{verbatim}
\begin{enumerate}
\item How many students are in the "students" dict? Use the appropriate function.
\item Implement a function that receives the students dict and returns the average age.
\item Implement a function that receives the students dict and an address, and returns a list with the name of all students which address matches the address in the argument. For instance, invoking "find\_students(students, 'Lisbon')" should return Peter and Anna.
\end{enumerate}