Skip to content

Commit 3ed219b

Browse files
committed
Publish Ch. 26
1 parent c517c14 commit 3ed219b

File tree

3 files changed

+281
-4
lines changed

3 files changed

+281
-4
lines changed
26.6 KB
Binary file not shown.
+277
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 26. Counting, compressing, and sorting\n",
8+
"\n",
9+
"\n",
10+
"## What are dictionaries for?\n",
11+
"\n",
12+
"\n",
13+
"[Learn Python with Jupyter](https://learnpythonwithjupyter.com/) by [Serena Bonaretti](https://sbonaretti.github.io/) \n",
14+
"Narrative license: [CC BY-NC-SA](https://creativecommons.org/licenses/by-nc-sa/2.0/) \n",
15+
"Code license: [GNU-GPL v3](https://www.gnu.org/licenses/gpl-3.0.en.html) "
16+
]
17+
},
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {},
21+
"source": [
22+
"--- \n",
23+
"## 1. Counting elements"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"- Given the following string:"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"greetings = \"hello! how are you?\""
40+
]
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"metadata": {},
45+
"source": [
46+
"- Create a dictionary where the keys are the letters of the alphabet found in the string, and the corresponding values are the number of times each letter is present. Write the code in two ways: (1) using `if`/`else`, and (2) using `.get()` \n",
47+
"\n",
48+
"1. Using `if`/`else`:"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": null,
54+
"metadata": {
55+
"scrolled": true
56+
},
57+
"outputs": [],
58+
"source": [
59+
"letter_counter = {}\n",
60+
"\n",
61+
"for letter in greetings:\n",
62+
" if letter not in letter_counter.keys():\n",
63+
" letter_counter[letter] = 1\n",
64+
" else:\n",
65+
" letter_counter[letter] += 1 \n",
66+
"\n",
67+
"for k, v in letter_counter.items():\n",
68+
" print (k, v)"
69+
]
70+
},
71+
{
72+
"cell_type": "markdown",
73+
"metadata": {
74+
"tags": []
75+
},
76+
"source": [
77+
"2. Using `.get()`:"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": null,
83+
"metadata": {},
84+
"outputs": [],
85+
"source": [
86+
"letter_counter = {}\n",
87+
"\n",
88+
"for letter in greetings:\n",
89+
" letter_counter[letter] = letter_counter.get(letter, 0) + 1\n",
90+
"\n",
91+
"for k, v in letter_counter.items():\n",
92+
" print (k, v) "
93+
]
94+
},
95+
{
96+
"cell_type": "markdown",
97+
"metadata": {},
98+
"source": [
99+
"---\n",
100+
"## 2. Compressing information"
101+
]
102+
},
103+
{
104+
"cell_type": "markdown",
105+
"metadata": {},
106+
"source": [
107+
"- Given the following list:"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": null,
113+
"metadata": {},
114+
"outputs": [],
115+
"source": [
116+
"sparse_vector = [0,0,0,1,0,7,0,0,4,0,0,0,8,0,0,0,6,0,0,0,0,0,0,0,9,0,0]"
117+
]
118+
},
119+
{
120+
"cell_type": "markdown",
121+
"metadata": {},
122+
"source": [
123+
"- Convert it into a dictionary:"
124+
]
125+
},
126+
{
127+
"cell_type": "code",
128+
"execution_count": null,
129+
"metadata": {},
130+
"outputs": [],
131+
"source": [
132+
"# create the dictionary\n",
133+
"sparse_dict = {}\n",
134+
"for i in range(len(sparse_vector)):\n",
135+
" if sparse_vector[i] != 0:\n",
136+
" sparse_dict[i] = sparse_vector[i]\n",
137+
"\n",
138+
"# save the list length \n",
139+
"sparse_dict[\"length\"] = len(sparse_vector) \n",
140+
" \n",
141+
"# print\n",
142+
"for k,v in sparse_dict.items():\n",
143+
" print (k,v) "
144+
]
145+
},
146+
{
147+
"cell_type": "markdown",
148+
"metadata": {},
149+
"source": [
150+
"- How do we get back to the sparse vector? "
151+
]
152+
},
153+
{
154+
"cell_type": "code",
155+
"execution_count": null,
156+
"metadata": {},
157+
"outputs": [],
158+
"source": [
159+
"# create a list of zeros\n",
160+
"sparse_vector_back = [0] * sparse_dict[\"length\"]\n",
161+
"\n",
162+
"# add nonzero values\n",
163+
"for k,v in sparse_dict.items():\n",
164+
" if k != \"length\":\n",
165+
" sparse_vector_back[k] = v \n",
166+
"\n",
167+
"# print\n",
168+
"print (sparse_vector_back)"
169+
]
170+
},
171+
{
172+
"cell_type": "markdown",
173+
"metadata": {},
174+
"source": [
175+
"---\n",
176+
"## 3. Sorting dictionary"
177+
]
178+
},
179+
{
180+
"cell_type": "markdown",
181+
"metadata": {},
182+
"source": [
183+
"- Given the following dictionary:"
184+
]
185+
},
186+
{
187+
"cell_type": "code",
188+
"execution_count": null,
189+
"metadata": {},
190+
"outputs": [],
191+
"source": [
192+
"registry = {\"Shaili\":4, \"Chris\":90, \"Maria\":70}"
193+
]
194+
},
195+
{
196+
"cell_type": "markdown",
197+
"metadata": {},
198+
"source": [
199+
"- Sort the dictionary items according to their *keys*:"
200+
]
201+
},
202+
{
203+
"cell_type": "code",
204+
"execution_count": null,
205+
"metadata": {},
206+
"outputs": [],
207+
"source": [
208+
"# create a new dictionary\n",
209+
"sorted_registry= {}\n",
210+
"\n",
211+
"# sort the keys\n",
212+
"sorted_keys = list(registry.keys())\n",
213+
"sorted_keys.sort()\n",
214+
"\n",
215+
"# fill out the new dictionary\n",
216+
"for k in sorted_keys:\n",
217+
" sorted_registry[k] = registry[k]\n",
218+
" \n",
219+
"print (sorted_registry)"
220+
]
221+
},
222+
{
223+
"cell_type": "markdown",
224+
"metadata": {},
225+
"source": [
226+
"- Sort the dictionary items according to their *values*:"
227+
]
228+
},
229+
{
230+
"cell_type": "code",
231+
"execution_count": null,
232+
"metadata": {},
233+
"outputs": [],
234+
"source": [
235+
"# create a new dictionary\n",
236+
"sorted_registry = {}\n",
237+
"\n",
238+
"# sort keys according to values \n",
239+
"sorted_keys = sorted(registry, key=registry.get) \n",
240+
"\n",
241+
"# fill out the new dictionary\n",
242+
"for k in sorted_keys:\n",
243+
" sorted_registry[k] = registry[k]\n",
244+
" \n",
245+
"print (sorted_registry)"
246+
]
247+
}
248+
],
249+
"metadata": {
250+
"kernelspec": {
251+
"display_name": "Python 3 (ipykernel)",
252+
"language": "python",
253+
"name": "python3"
254+
},
255+
"language_info": {
256+
"codemirror_mode": {
257+
"name": "ipython",
258+
"version": 3
259+
},
260+
"file_extension": ".py",
261+
"mimetype": "text/x-python",
262+
"name": "python",
263+
"nbconvert_exporter": "python",
264+
"pygments_lexer": "ipython3",
265+
"version": "3.11.7"
266+
},
267+
"widgets": {
268+
"application/vnd.jupyter.widget-state+json": {
269+
"state": {},
270+
"version_major": 2,
271+
"version_minor": 0
272+
}
273+
}
274+
},
275+
"nbformat": 4,
276+
"nbformat_minor": 4
277+
}

index.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,12 @@ <h2>About the Book</h2>
235235
<div class="col-lg-12">
236236

237237
<div class="progress">
238-
<span class="skill">BOOK COMPLETION <i class="val">68%</i></span>
238+
<span class="skill">BOOK COMPLETION <i class="val">70%</i></span>
239239
<div class="progress-bar-wrap">
240-
<div class="progress-bar" role="progressbar" aria-valuenow="68" aria-valuemin="0" aria-valuemax="100"></div>
240+
<div class="progress-bar" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100"></div>
241241
</div>
242242
</div>
243-
<center>Next upload: <b>Chapter 26</b> on <b>April 24</b> </center>
243+
<center>Next upload: <b>Chapter 27</b> on <b>June 23</b> </center>
244244
</div>
245245

246246
</div>
@@ -339,7 +339,7 @@ <h4>PART 7: Dictionaries and overview of strings</h4>
339339
<ol start="24">
340340
<a href="assets/notebooks/24_dictionaries.ipynb" onclick="download" ><li>Inventory at the English bookstore<br> <i>Dictionaries</i></li></a>
341341
<a href="assets/notebooks/25_dictionary_list.ipynb" onclick="download" ><li>Trip to Switzerland<br> <i>Dictionaries with lists as values</i></li></a>
342-
<li>Counting, compressing, and sorting<br> <i>Dictionary use cases</i></li>
342+
<a href="assets/notebooks/26_dictionary_use.ipynb" onclick="download" ><li>Counting, compressing, and sorting<br> <i>What are dictionaries for?</i></li></a>
343343
<li>Overview of strings<br> <i>Operations, methods, and tricks</i></li>
344344
</ol>
345345
</div>

0 commit comments

Comments
 (0)