Skip to content

Commit 1fb82d5

Browse files
committed
adding dict elements
1 parent e342e59 commit 1fb82d5

2 files changed

Lines changed: 348 additions & 12 deletions

File tree

.ipynb_checkpoints/timeit_test-checkpoint.ipynb

Lines changed: 129 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:30bb811f79dc78fe63221c8960bb4237c811b94fdf197b6479709aff3244cb08"
4+
"signature": "sha256:e6a1d9a637dfbad45211a98a5bbc35255dc8f168a834ed466c363dfc384c0f59"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -19,7 +19,31 @@
1919
"cell_type": "markdown",
2020
"metadata": {},
2121
"source": [
22-
"### String formatting - `.format()` vs. `%s`"
22+
"# Sections\n",
23+
"- [String formatting](#string_formatting)\n",
24+
" - [String formatting - .format() vs. binary operator %s](#str_format_bin)\n",
25+
" - [String Reversing - [::-1] vs. `''.join(reversed())`](#str_reverse)\n",
26+
"- [List Operations](#list_operations)\n",
27+
" - [List Reversing - [::-1] vs. reverse() vs. reversed()](#list_reverse)\n",
28+
"- [Dictionary Operations](#dict_ops) \n",
29+
" - [Adding elements to a dictionary](#adding_dict_elements)"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"<a name='string_formatting'></a>\n",
37+
"\n",
38+
"# String formatting"
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"metadata": {},
44+
"source": [
45+
"<a name='str_format_bin'></a>\n",
46+
"### String formatting - `.format()` vs. binary operator `%s`"
2347
]
2448
},
2549
{
@@ -74,6 +98,7 @@
7498
"cell_type": "markdown",
7599
"metadata": {},
76100
"source": [
101+
"<a name='str_reverse'></a>\n",
77102
"### String Reversing - `[::-1]` vs. `''.join(reversed())`"
78103
]
79104
},
@@ -129,6 +154,15 @@
129154
"cell_type": "markdown",
130155
"metadata": {},
131156
"source": [
157+
"<a name='list_operations'></a>\n",
158+
"# List Operations"
159+
]
160+
},
161+
{
162+
"cell_type": "markdown",
163+
"metadata": {},
164+
"source": [
165+
"<a name='list_reverse'></a>\n",
132166
"### List Reversing - `[::-1]` vs. `reverse()` vs. `reversed()`"
133167
]
134168
},
@@ -166,16 +200,16 @@
166200
"output_type": "stream",
167201
"stream": "stdout",
168202
"text": [
169-
"1000000 loops, best of 3: 857 ns per loop\n",
170-
"1000000 loops, best of 3: 1.78 \u00b5s per loop"
203+
"1000000 loops, best of 3: 930 ns per loop\n",
204+
"1000000 loops, best of 3: 1.89 \u00b5s per loop"
171205
]
172206
},
173207
{
174208
"output_type": "stream",
175209
"stream": "stdout",
176210
"text": [
177211
"\n",
178-
"1000000 loops, best of 3: 758 ns per loop"
212+
"1000000 loops, best of 3: 775 ns per loop"
179213
]
180214
},
181215
{
@@ -186,7 +220,96 @@
186220
]
187221
}
188222
],
189-
"prompt_number": 10
223+
"prompt_number": 1
224+
},
225+
{
226+
"cell_type": "markdown",
227+
"metadata": {},
228+
"source": [
229+
"<a name='dict_ops'></a>\n",
230+
"# Dictionary Operations "
231+
]
232+
},
233+
{
234+
"cell_type": "markdown",
235+
"metadata": {},
236+
"source": [
237+
"<a name='adding_dict_elements'></a>\n",
238+
"## Adding elements to a Dictionary"
239+
]
240+
},
241+
{
242+
"cell_type": "code",
243+
"collapsed": false,
244+
"input": [
245+
"import random\n",
246+
"\n",
247+
"random.seed(123)\n",
248+
"rand_ints = [randrange(1, 10) for i in range(100)]\n",
249+
"\n",
250+
"def add_element_check1(my_dict, elements):\n",
251+
" for e in elements:\n",
252+
" if e not in my_dict:\n",
253+
" my_dict[e] = 1\n",
254+
" else:\n",
255+
" my_dict[e] += 1\n",
256+
" \n",
257+
"def add_element_check2(my_dict, elements):\n",
258+
" for e in elements:\n",
259+
" if e not in my_dict:\n",
260+
" my_dict[e] = 0\n",
261+
" my_dict[e] += 1 \n",
262+
"\n",
263+
"def add_element_except(my_dict):\n",
264+
" for e in elements:\n",
265+
" try:\n",
266+
" elements[e] += 1\n",
267+
" except KeyError:\n",
268+
" elements[e] = 1\n",
269+
" "
270+
],
271+
"language": "python",
272+
"metadata": {},
273+
"outputs": [
274+
{
275+
"output_type": "stream",
276+
"stream": "stdout",
277+
"text": [
278+
"1\n",
279+
"5\n",
280+
"2\n",
281+
"7\n",
282+
"5\n",
283+
"2\n",
284+
"1\n",
285+
"7\n",
286+
"9\n",
287+
"9\n",
288+
"\n",
289+
"\n",
290+
"6\n",
291+
"6\n",
292+
"1\n",
293+
"3\n",
294+
"3\n",
295+
"6\n",
296+
"9\n",
297+
"6\n",
298+
"4\n",
299+
"3\n"
300+
]
301+
}
302+
],
303+
"prompt_number": 16
304+
},
305+
{
306+
"cell_type": "code",
307+
"collapsed": false,
308+
"input": [],
309+
"language": "python",
310+
"metadata": {},
311+
"outputs": [],
312+
"prompt_number": 15
190313
},
191314
{
192315
"cell_type": "code",

0 commit comments

Comments
 (0)