Skip to content

Commit 59818e0

Browse files
committed
ch03: wip
1 parent 8a1b6fe commit 59818e0

File tree

5 files changed

+430
-8
lines changed

5 files changed

+430
-8
lines changed

03-dict-set/03-dict-set.ipynb

Lines changed: 385 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,385 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) \n",
13+
"[Clang 6.0 (clang-600.0.57)]\n"
14+
]
15+
}
16+
],
17+
"source": [
18+
"import sys\n",
19+
"print(sys.version)"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 2,
25+
"metadata": {},
26+
"outputs": [
27+
{
28+
"data": {
29+
"text/plain": [
30+
"True"
31+
]
32+
},
33+
"execution_count": 2,
34+
"metadata": {},
35+
"output_type": "execute_result"
36+
}
37+
],
38+
"source": [
39+
"from collections import abc\n",
40+
"my_dict = {}\n",
41+
"isinstance(my_dict, abc.Mapping)"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 3,
47+
"metadata": {},
48+
"outputs": [
49+
{
50+
"data": {
51+
"text/plain": [
52+
"True"
53+
]
54+
},
55+
"execution_count": 3,
56+
"metadata": {},
57+
"output_type": "execute_result"
58+
}
59+
],
60+
"source": [
61+
"isinstance(my_dict, abc.MutableMapping)"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": 4,
67+
"metadata": {},
68+
"outputs": [
69+
{
70+
"data": {
71+
"text/plain": [
72+
"-3907003130834322577"
73+
]
74+
},
75+
"execution_count": 4,
76+
"metadata": {},
77+
"output_type": "execute_result"
78+
}
79+
],
80+
"source": [
81+
"tt = (1, 2, (30, 40))\n",
82+
"hash(tt)"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": 7,
88+
"metadata": {},
89+
"outputs": [
90+
{
91+
"name": "stdout",
92+
"output_type": "stream",
93+
"text": [
94+
"unhashable type: 'list'\n"
95+
]
96+
}
97+
],
98+
"source": [
99+
"tl = (1, 2, [30, 40])\n",
100+
"try:\n",
101+
" hash(tl)\n",
102+
"except TypeError as e:\n",
103+
" print(e)"
104+
]
105+
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": 5,
109+
"metadata": {},
110+
"outputs": [
111+
{
112+
"data": {
113+
"text/plain": [
114+
"5149391500123939311"
115+
]
116+
},
117+
"execution_count": 5,
118+
"metadata": {},
119+
"output_type": "execute_result"
120+
}
121+
],
122+
"source": [
123+
"tf = (1, 2, frozenset([30, 40]))\n",
124+
"hash(tf)"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": 8,
130+
"metadata": {},
131+
"outputs": [
132+
{
133+
"data": {
134+
"text/plain": [
135+
"True"
136+
]
137+
},
138+
"execution_count": 8,
139+
"metadata": {},
140+
"output_type": "execute_result"
141+
}
142+
],
143+
"source": [
144+
"a = dict(one=1, two=2, three=3)\n",
145+
"b = {'three': 3, 'two': 2, 'one': 1}\n",
146+
"c = dict([('two', 2), ('one', 1), ('three', 3)])\n",
147+
"d = dict(zip(['one', 'two', 'three'], [1, 2, 3]))\n",
148+
"e = dict({'three': 3, 'one': 1, 'two': 2})\n",
149+
"a == b == c == d == e"
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": 10,
155+
"metadata": {},
156+
"outputs": [
157+
{
158+
"data": {
159+
"text/plain": [
160+
"{'one': 1, 'two': 2, 'three': 3}"
161+
]
162+
},
163+
"execution_count": 10,
164+
"metadata": {},
165+
"output_type": "execute_result"
166+
}
167+
],
168+
"source": [
169+
"a"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": 11,
175+
"metadata": {},
176+
"outputs": [
177+
{
178+
"data": {
179+
"text/plain": [
180+
"['one', 'two', 'three']"
181+
]
182+
},
183+
"execution_count": 11,
184+
"metadata": {},
185+
"output_type": "execute_result"
186+
}
187+
],
188+
"source": [
189+
"list(a.keys())"
190+
]
191+
},
192+
{
193+
"cell_type": "code",
194+
"execution_count": 12,
195+
"metadata": {},
196+
"outputs": [
197+
{
198+
"data": {
199+
"text/plain": [
200+
"{'two': 2, 'one': 1, 'three': 3}"
201+
]
202+
},
203+
"execution_count": 12,
204+
"metadata": {},
205+
"output_type": "execute_result"
206+
}
207+
],
208+
"source": [
209+
"c"
210+
]
211+
},
212+
{
213+
"cell_type": "code",
214+
"execution_count": 13,
215+
"metadata": {},
216+
"outputs": [
217+
{
218+
"data": {
219+
"text/plain": [
220+
"('three', 3)"
221+
]
222+
},
223+
"execution_count": 13,
224+
"metadata": {},
225+
"output_type": "execute_result"
226+
}
227+
],
228+
"source": [
229+
"c.popitem()"
230+
]
231+
},
232+
{
233+
"cell_type": "code",
234+
"execution_count": 14,
235+
"metadata": {},
236+
"outputs": [
237+
{
238+
"data": {
239+
"text/plain": [
240+
"{'two': 2, 'one': 1}"
241+
]
242+
},
243+
"execution_count": 14,
244+
"metadata": {},
245+
"output_type": "execute_result"
246+
}
247+
],
248+
"source": [
249+
"c"
250+
]
251+
},
252+
{
253+
"cell_type": "code",
254+
"execution_count": 15,
255+
"metadata": {},
256+
"outputs": [],
257+
"source": [
258+
"dial_codes = [ # <1>\n",
259+
" (880, 'Bangladesh'),\n",
260+
" (55, 'Brazil'),\n",
261+
" (86, 'China'),\n",
262+
" (91, 'India'),\n",
263+
" (62, 'Indonesia'),\n",
264+
" (81, 'Japan'),\n",
265+
" (234, 'Nigeria'),\n",
266+
" (92, 'Pakistan'),\n",
267+
" (7, 'Russia'),\n",
268+
" (1, 'United States'),\n",
269+
"]"
270+
]
271+
},
272+
{
273+
"cell_type": "code",
274+
"execution_count": 17,
275+
"metadata": {},
276+
"outputs": [
277+
{
278+
"data": {
279+
"text/plain": [
280+
"{'Bangladesh': 880,\n",
281+
" 'Brazil': 55,\n",
282+
" 'China': 86,\n",
283+
" 'India': 91,\n",
284+
" 'Indonesia': 62,\n",
285+
" 'Japan': 81,\n",
286+
" 'Nigeria': 234,\n",
287+
" 'Pakistan': 92,\n",
288+
" 'Russia': 7,\n",
289+
" 'United States': 1}"
290+
]
291+
},
292+
"execution_count": 17,
293+
"metadata": {},
294+
"output_type": "execute_result"
295+
}
296+
],
297+
"source": [
298+
"country_dial = {country: code for code, country in dial_codes}\n",
299+
"country_dial"
300+
]
301+
},
302+
{
303+
"cell_type": "code",
304+
"execution_count": 18,
305+
"metadata": {},
306+
"outputs": [
307+
{
308+
"data": {
309+
"text/plain": [
310+
"{55: 'BRAZIL', 62: 'INDONESIA', 7: 'RUSSIA', 1: 'UNITED STATES'}"
311+
]
312+
},
313+
"execution_count": 18,
314+
"metadata": {},
315+
"output_type": "execute_result"
316+
}
317+
],
318+
"source": [
319+
"{code: country.upper() \n",
320+
" for country, code in sorted(country_dial.items())\n",
321+
" if code < 70}"
322+
]
323+
},
324+
{
325+
"cell_type": "code",
326+
"execution_count": 20,
327+
"metadata": {},
328+
"outputs": [
329+
{
330+
"data": {
331+
"text/plain": [
332+
"{'Pakistan': 92,\n",
333+
" 'Indonesia': 62,\n",
334+
" 'Russia': 7,\n",
335+
" 'Japan': 81,\n",
336+
" 'United States': 1,\n",
337+
" 'China': 86,\n",
338+
" 'Brazil': 55,\n",
339+
" 'Bangladesh': 880,\n",
340+
" 'Nigeria': 234,\n",
341+
" 'India': 91}"
342+
]
343+
},
344+
"execution_count": 20,
345+
"metadata": {},
346+
"output_type": "execute_result"
347+
}
348+
],
349+
"source": [
350+
"from random import shuffle\n",
351+
"shuffle(dial_codes)\n",
352+
"country_dial = {country: code for code, country in dial_codes}\n",
353+
"country_dial"
354+
]
355+
},
356+
{
357+
"cell_type": "code",
358+
"execution_count": null,
359+
"metadata": {},
360+
"outputs": [],
361+
"source": []
362+
}
363+
],
364+
"metadata": {
365+
"kernelspec": {
366+
"display_name": "Python 3",
367+
"language": "python",
368+
"name": "python3"
369+
},
370+
"language_info": {
371+
"codemirror_mode": {
372+
"name": "ipython",
373+
"version": 3
374+
},
375+
"file_extension": ".py",
376+
"mimetype": "text/x-python",
377+
"name": "python",
378+
"nbconvert_exporter": "python",
379+
"pygments_lexer": "ipython3",
380+
"version": "3.8.0"
381+
}
382+
},
383+
"nbformat": 4,
384+
"nbformat_minor": 2
385+
}

0 commit comments

Comments
 (0)