-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides-08-01.qmd
335 lines (238 loc) · 6.19 KB
/
slides-08-01.qmd
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
---
title: "intro to dictionaries (slides)"
format: revealjs
slide-number: true
df-print: kable
---
# CSc 110 - dictionaries
## Announcements
* Last lab for short project 06 is Today -- no lab for the rest of the week, we re-start lab sessions Tuesday October 22
# Dictionaries
## Data Structure
- **A data-structure is a way of arranging and organizing data in a computer program**
- Python has several useful data-structures built into the language
- One is a **list** (already covered)
- Another, **dictionary**
## Mapping
- Many data structures allow data to be stored and retrieved using a concept called **mapping**
- **Mapping** is the process of associating one value with another (a **key** with a **value**)
- Sometimes also referred to as Hashing or Associativity
## Mapping
- Lists map **keys** to **values** too!
- Indices **of** the list are the **keys**
- Elements **in** the list are the **values**
- Keys (indices) are used to acess or modify the elements in the list

## Mapping and Lists
```{python}
#| eval: false
#| echo: true
numbers = [12, 49, -2, 26, 5, 17, -6]
```
- What are the keys?
- What are the values?
- Which keys map to which values?
## Mapping and Lists
```{python}
#| eval: false
#| echo: true
numbers = [12, 49, -2, 26, 5, 17, -6]
# Using the key 3 to lookup the associated value of 26
# and then save the value into variable
new = numbers[3]
# Modifying the list so that the key 5 now maps to 77
# instead of 17
numbers[5] = 77
```
## Dictionary
- Like lists:
- Associates a set of keys to their corresponding values
- Each key has exactly 1 associated value
- Unlike lists:
- The keys can be types other than ints: strings
## Dictionary
Example (mapping strings to integers)
```{python}
#| eval: true
#| echo: true
singers = { "Taylor Swift": 1989,
"JVKE": 2001,
"Bruno Mars": 1983}
# Using the key "JVKE"
# to lookup the number 2001
singers["JVKE"]
# Modifying the value associated with "Bruno Mars"
singers["Bruno Mars"] = 1985
# add a new key and value pair
singers["Doja Cat"] = 1995
print(singers)
```
## Evaluate the expressions
```{python}
#| eval: false
#| echo: true
word_count = {"and": 324, "why": 134, "cannot": 76, "sanded": 13}
word_count["cannot"] = 90
word_count["and"] = 110
word_count["foot"] = "feet"
word_count["and"] += 10
# what will these evaluate to?
word_count["and"]
word_count["cannot"]
word_count["foot"]
```
## Evaluate the expressions
```{python}
#| eval: true
#| echo: true
word_count = {"and": 324, "why": 134, "cannot": 76, "sanded": 13}
word_count["cannot"] = 90
word_count["and"] = 110
word_count["foot"] = "feet"
word_count["and"] += 10
# what will these evaluate to?
word_count["and"]
```
```{python}
#| eval: true
#| echo: true
word_count["cannot"]
```
```{python}
#| eval: true
#| echo: true
word_count["foot"]
```
## Evaluate the expressions
```{python}
#| eval: false
#| echo: true
num_to_player = {} # A valid, but empty dictionary
num_to_player[13] = "Paul George"
num_to_player[3] = "Chris Paul"
num_to_player[23] = "Lebron James"
num_to_player[13] = "James Harden"
# what will these evaluate to?
num_to_player[23]
num_to_player[3]
num_to_player[13]
num_to_player
```
## Evaluate the expressions
```{python}
#| eval: true
#| echo: true
num_to_player = {} # A valid, but empty dictionary
num_to_player[13] = "Paul George"
num_to_player[3] = "Chris Paul"
num_to_player[23] = "Lebron James"
num_to_player[13] = "James Harden"
# what will these evaluate to?
num_to_player[23]
```
```{python}
#| eval: true
#| echo: true
num_to_player[3]
```
```{python}
#| eval: true
#| echo: true
num_to_player[13]
```
```{python}
#| eval: true
#| echo: true
num_to_player
```
## Attendance
Attendance Evaluate the expression on Gradescope.
## Review: list methods
- `.append(value)`
- `.remove(value)`
- `.pop(index)`
## Dictionary operations
```{python}
#| eval: false
#| echo: true
scores = {'A': 10, 'B': 25, 'C': 27, 'D': 10, 'E': 5}
scores['A+'] = 7 # Adds a key/value pair
scores['B'] = 20 # Changes value associated with a key
scores['C'] # Retrieves a value, given a key
scores.pop('E') # Removes a key/value pair
```
## The `in` operator
With strings:
```{python}
#| eval: true
#| echo: true
"a" in "aeiou"
```
With lists:
```{python}
#| eval: true
#| echo: true
1 in [1, 2, 3, 4, 5]
```
And dictionary keys:
```{python}
#| eval: true
#| echo: true
word_count = {"and": 324, "why": 134, "cannot": 76, "Sanded": 13}
"why" in word_count
```
## Write a function
Its name is `count_vowels` that takes a `string` argument
It creates a `dictionary` and returns the `dictionary` with the count of every lowercase vowel in `string` (iterate over the string with a `for` loop)
Hint: initialize a dictionary with all the vowels as keys and the associated count as 0.
```{python}
#| eval: false
#| echo: true
assert count_vowels("") == {"a": 0, "e": 0, "i": 0, "o": 0, "u": 0}
assert count_vowels("pineapple") == {"a": 1, "e": 2, "i": 1, "o": 0, "u": 0}
```
## Write a function -- solution
```{python}
#| eval: true
#| echo: true
def count_vowels(string):
counts = {"a": 0, "e": 0, "i": 0, "o": 0, "u": 0}
for i in range(len(string)):
char = string[i]
if char in counts:
counts[char] += 1
return counts
def main():
assert count_vowels("") == {"a": 0, "e": 0, "i": 0, "o": 0, "u": 0}
assert count_vowels("pineapple") == {"a": 1, "e": 2, "i": 1, "o": 0, "u": 0}
main()
```
## Write a function
1. Its name is `count_chars`
2. It takes a `string` argument
3. It creates a `dictionary`
4. It returns the `dictionary` with the count of every characters in `string`
```{python}
#| eval: false
#| echo: true
print( count_chars("") ) # {}
print( count_chars("banana") ) # {"b": 1, "a": 3, "n": 2}
```
## Write a function -- solution
```{python}
#| eval: true
#| echo: true
def count_chars(string):
counts = {}
for i in range(len(string)):
char = string[i]
if char in counts:
counts[char] += 1
else:
counts[char] = 1
return counts
def main():
print( count_chars("") ) # {}
print( count_chars("banana") ) # {"b": 1, "a": 3, "n": 2}
main()
```