Skip to content

Commit 83bf299

Browse files
committed
Day 1s meanderins
1 parent a1eac90 commit 83bf299

File tree

1 file changed

+345
-0
lines changed

1 file changed

+345
-0
lines changed

days/04-06-collections/Day1.ipynb

Lines changed: 345 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"Another good resource:\n",
8+
"\n",
9+
"https://howchoo.com/g/mtbhy2qzota/python-collections\n"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 28,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"from collections import namedtuple, defaultdict"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 3,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"User = namedtuple('user','name job age')"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 14,
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"user = User(name = 'Bill',job = 'Dad',age = 36)"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": 19,
42+
"metadata": {},
43+
"outputs": [
44+
{
45+
"data": {
46+
"text/plain": [
47+
"'Bill'"
48+
]
49+
},
50+
"execution_count": 19,
51+
"metadata": {},
52+
"output_type": "execute_result"
53+
}
54+
],
55+
"source": [
56+
"user.name"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 18,
62+
"metadata": {},
63+
"outputs": [
64+
{
65+
"data": {
66+
"text/plain": [
67+
"'Bill is a Dad'"
68+
]
69+
},
70+
"execution_count": 18,
71+
"metadata": {},
72+
"output_type": "execute_result"
73+
}
74+
],
75+
"source": [
76+
"f'{user.name} is a {user.job}'"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": 35,
82+
"metadata": {},
83+
"outputs": [
84+
{
85+
"data": {
86+
"text/plain": [
87+
"[('mike', 10),\n",
88+
" ('julian', 7),\n",
89+
" ('bob', 5),\n",
90+
" ('mike', 11),\n",
91+
" ('julian', 8),\n",
92+
" ('bob', 6),\n",
93+
" ('bill', 40)]"
94+
]
95+
},
96+
"execution_count": 35,
97+
"metadata": {},
98+
"output_type": "execute_result"
99+
}
100+
],
101+
"source": [
102+
"challenges_done = [('mike', 10), ('julian', 7), ('bob', 5),\n",
103+
" ('mike', 11), ('julian', 8), ('bob', 6),\n",
104+
" ('bill', 40)\n",
105+
" ]\n",
106+
"challenges_done"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 23,
112+
"metadata": {},
113+
"outputs": [
114+
{
115+
"name": "stdout",
116+
"output_type": "stream",
117+
"text": [
118+
"mike 10\n",
119+
"julian 7\n",
120+
"bob 5\n",
121+
"mike 11\n",
122+
"julian 8\n",
123+
"bob 6\n"
124+
]
125+
}
126+
],
127+
"source": [
128+
"for name, challenge in challenges_done:\n",
129+
" print(name, challenge)"
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": 73,
135+
"metadata": {},
136+
"outputs": [],
137+
"source": [
138+
"challenges_to_do = [('bill',10),('rachel',10),('bill',23)]\n"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": 75,
144+
"metadata": {},
145+
"outputs": [
146+
{
147+
"data": {
148+
"text/plain": [
149+
"defaultdict(int, {'bill': 33, 'rachel': 10})"
150+
]
151+
},
152+
"execution_count": 75,
153+
"metadata": {},
154+
"output_type": "execute_result"
155+
}
156+
],
157+
"source": [
158+
"challenges = defaultdict(int)\n",
159+
"for name, challenge in challenges_to_do:\n",
160+
" challenges[name] += challenge\n",
161+
"\n",
162+
"challenges"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": 25,
168+
"metadata": {},
169+
"outputs": [
170+
{
171+
"name": "stdout",
172+
"output_type": "stream",
173+
"text": [
174+
"1\n",
175+
"1\n"
176+
]
177+
}
178+
],
179+
"source": [
180+
"for name, challenge, i in challenges_to_do:\n",
181+
" print(i)"
182+
]
183+
},
184+
{
185+
"cell_type": "code",
186+
"execution_count": 26,
187+
"metadata": {},
188+
"outputs": [
189+
{
190+
"ename": "KeyError",
191+
"evalue": "'mike'",
192+
"output_type": "error",
193+
"traceback": [
194+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
195+
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
196+
"\u001b[0;32m<ipython-input-26-9f3e2914c8dc>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mchallenges\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mchallenge\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mchallenges_done\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mchallenges\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mchallenge\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
197+
"\u001b[0;31mKeyError\u001b[0m: 'mike'"
198+
]
199+
}
200+
],
201+
"source": [
202+
"# Building up a dictionary is impossible(?) because the key doesn't exist - or you need to do the if statement\n",
203+
"\n",
204+
"challenges = {}\n",
205+
"for name, challenge in challenges_done:\n",
206+
" challenges[name].append(challenge)"
207+
]
208+
},
209+
{
210+
"cell_type": "markdown",
211+
"metadata": {},
212+
"source": [
213+
"Can do it pretty easily with a defaultdict"
214+
]
215+
},
216+
{
217+
"cell_type": "code",
218+
"execution_count": 56,
219+
"metadata": {},
220+
"outputs": [
221+
{
222+
"data": {
223+
"text/plain": [
224+
"defaultdict(list,\n",
225+
" {'mike': [10, 11], 'julian': [7, 8], 'bob': [5, 6], 'bill': [40]})"
226+
]
227+
},
228+
"execution_count": 56,
229+
"metadata": {},
230+
"output_type": "execute_result"
231+
}
232+
],
233+
"source": [
234+
"challenges = defaultdict(list)\n",
235+
"for name, challenge in challenges_done:\n",
236+
" challenges[name].append(challenge)\n",
237+
"\n",
238+
"challenges"
239+
]
240+
},
241+
{
242+
"cell_type": "code",
243+
"execution_count": 34,
244+
"metadata": {},
245+
"outputs": [
246+
{
247+
"name": "stdout",
248+
"output_type": "stream",
249+
"text": [
250+
"mike 10\n",
251+
"julian 7\n",
252+
"bob 5\n"
253+
]
254+
}
255+
],
256+
"source": [
257+
"for k,v in challenges.items():\n",
258+
" print(k,v[0])"
259+
]
260+
},
261+
{
262+
"cell_type": "code",
263+
"execution_count": 39,
264+
"metadata": {},
265+
"outputs": [
266+
{
267+
"data": {
268+
"text/plain": [
269+
"defaultdict(int, {})"
270+
]
271+
},
272+
"execution_count": 39,
273+
"metadata": {},
274+
"output_type": "execute_result"
275+
}
276+
],
277+
"source": [
278+
"challenges_to_do"
279+
]
280+
},
281+
{
282+
"cell_type": "code",
283+
"execution_count": 70,
284+
"metadata": {},
285+
"outputs": [
286+
{
287+
"name": "stdout",
288+
"output_type": "stream",
289+
"text": [
290+
"0\n"
291+
]
292+
}
293+
],
294+
"source": [
295+
"someddict = defaultdict(int)\n",
296+
"print(someddict[3])"
297+
]
298+
},
299+
{
300+
"cell_type": "code",
301+
"execution_count": 71,
302+
"metadata": {},
303+
"outputs": [
304+
{
305+
"name": "stdout",
306+
"output_type": "stream",
307+
"text": [
308+
"defaultdict(<class 'int'>, {3: 0})\n"
309+
]
310+
}
311+
],
312+
"source": [
313+
"print(someddict)"
314+
]
315+
},
316+
{
317+
"cell_type": "code",
318+
"execution_count": null,
319+
"metadata": {},
320+
"outputs": [],
321+
"source": []
322+
}
323+
],
324+
"metadata": {
325+
"kernelspec": {
326+
"display_name": "Python 3",
327+
"language": "python",
328+
"name": "python3"
329+
},
330+
"language_info": {
331+
"codemirror_mode": {
332+
"name": "ipython",
333+
"version": 3
334+
},
335+
"file_extension": ".py",
336+
"mimetype": "text/x-python",
337+
"name": "python",
338+
"nbconvert_exporter": "python",
339+
"pygments_lexer": "ipython3",
340+
"version": "3.6.6"
341+
}
342+
},
343+
"nbformat": 4,
344+
"nbformat_minor": 2
345+
}

0 commit comments

Comments
 (0)