adding dict elements

This commit is contained in:
rasbt 2014-04-14 11:48:34 -04:00
parent e342e59c66
commit 1fb82d58f3
2 changed files with 348 additions and 12 deletions

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:30bb811f79dc78fe63221c8960bb4237c811b94fdf197b6479709aff3244cb08"
"signature": "sha256:e6a1d9a637dfbad45211a98a5bbc35255dc8f168a834ed466c363dfc384c0f59"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -19,7 +19,31 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### String formatting - `.format()` vs. `%s`"
"# Sections\n",
"- [String formatting](#string_formatting)\n",
" - [String formatting - .format() vs. binary operator %s](#str_format_bin)\n",
" - [String Reversing - [::-1] vs. `''.join(reversed())`](#str_reverse)\n",
"- [List Operations](#list_operations)\n",
" - [List Reversing - [::-1] vs. reverse() vs. reversed()](#list_reverse)\n",
"- [Dictionary Operations](#dict_ops) \n",
" - [Adding elements to a dictionary](#adding_dict_elements)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='string_formatting'></a>\n",
"\n",
"# String formatting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='str_format_bin'></a>\n",
"### String formatting - `.format()` vs. binary operator `%s`"
]
},
{
@ -74,6 +98,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='str_reverse'></a>\n",
"### String Reversing - `[::-1]` vs. `''.join(reversed())`"
]
},
@ -129,6 +154,15 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='list_operations'></a>\n",
"# List Operations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='list_reverse'></a>\n",
"### List Reversing - `[::-1]` vs. `reverse()` vs. `reversed()`"
]
},
@ -166,8 +200,8 @@
"output_type": "stream",
"stream": "stdout",
"text": [
"1000000 loops, best of 3: 857 ns per loop\n",
"1000000 loops, best of 3: 1.78 \u00b5s per loop"
"1000000 loops, best of 3: 930 ns per loop\n",
"1000000 loops, best of 3: 1.89 \u00b5s per loop"
]
},
{
@ -175,7 +209,7 @@
"stream": "stdout",
"text": [
"\n",
"1000000 loops, best of 3: 758 ns per loop"
"1000000 loops, best of 3: 775 ns per loop"
]
},
{
@ -186,7 +220,96 @@
]
}
],
"prompt_number": 10
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='dict_ops'></a>\n",
"# Dictionary Operations "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='adding_dict_elements'></a>\n",
"## Adding elements to a Dictionary"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import random\n",
"\n",
"random.seed(123)\n",
"rand_ints = [randrange(1, 10) for i in range(100)]\n",
"\n",
"def add_element_check1(my_dict, elements):\n",
" for e in elements:\n",
" if e not in my_dict:\n",
" my_dict[e] = 1\n",
" else:\n",
" my_dict[e] += 1\n",
" \n",
"def add_element_check2(my_dict, elements):\n",
" for e in elements:\n",
" if e not in my_dict:\n",
" my_dict[e] = 0\n",
" my_dict[e] += 1 \n",
"\n",
"def add_element_except(my_dict):\n",
" for e in elements:\n",
" try:\n",
" elements[e] += 1\n",
" except KeyError:\n",
" elements[e] = 1\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"5\n",
"2\n",
"7\n",
"5\n",
"2\n",
"1\n",
"7\n",
"9\n",
"9\n",
"\n",
"\n",
"6\n",
"6\n",
"1\n",
"3\n",
"3\n",
"6\n",
"9\n",
"6\n",
"4\n",
"3\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
},
{
"cell_type": "code",

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:30bb811f79dc78fe63221c8960bb4237c811b94fdf197b6479709aff3244cb08"
"signature": "sha256:629f08ba90badbccee0e3581db0e76c716d0b93abc92869d2dc5176ac6b02854"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -19,7 +19,31 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### String formatting - `.format()` vs. `%s`"
"# Sections\n",
"- [String formatting](#string_formatting)\n",
" - [String formatting - .format() vs. binary operator %s](#str_format_bin)\n",
" - [String Reversing - [::-1] vs. `''.join(reversed())`](#str_reverse)\n",
"- [List Operations](#list_operations)\n",
" - [List Reversing - [::-1] vs. reverse() vs. reversed()](#list_reverse)\n",
"- [Dictionary Operations](#dict_ops) \n",
" - [Adding elements to a dictionary](#adding_dict_elements)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='string_formatting'></a>\n",
"\n",
"# String formatting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='str_format_bin'></a>\n",
"### String formatting - `.format()` vs. binary operator `%s`"
]
},
{
@ -74,6 +98,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='str_reverse'></a>\n",
"### String Reversing - `[::-1]` vs. `''.join(reversed())`"
]
},
@ -129,6 +154,15 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='list_operations'></a>\n",
"# List Operations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='list_reverse'></a>\n",
"### List Reversing - `[::-1]` vs. `reverse()` vs. `reversed()`"
]
},
@ -166,8 +200,8 @@
"output_type": "stream",
"stream": "stdout",
"text": [
"1000000 loops, best of 3: 857 ns per loop\n",
"1000000 loops, best of 3: 1.78 \u00b5s per loop"
"1000000 loops, best of 3: 930 ns per loop\n",
"1000000 loops, best of 3: 1.89 \u00b5s per loop"
]
},
{
@ -175,7 +209,7 @@
"stream": "stdout",
"text": [
"\n",
"1000000 loops, best of 3: 758 ns per loop"
"1000000 loops, best of 3: 775 ns per loop"
]
},
{
@ -186,7 +220,186 @@
]
}
],
"prompt_number": 10
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='dict_ops'></a>\n",
"# Dictionary Operations "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='adding_dict_elements'></a>\n",
"## Adding elements to a Dictionary\n",
"\n",
"All three functions below count how often different elements (values) occur in a list. \n",
"E.g., for the list ['a', 'b', 'a', 'c'], the dictionary would look like this: \n",
"`my_dict = {'a': 2, 'b': 1, 'c': 1}`"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import random\n",
"import copy\n",
"import timeit\n",
"\n",
"\n",
"\n",
"def add_element_check1(my_dict, elements):\n",
" for e in elements:\n",
" if e not in my_dict:\n",
" my_dict[e] = 1\n",
" else:\n",
" my_dict[e] += 1\n",
" \n",
"def add_element_check2(my_dict, elements):\n",
" for e in elements:\n",
" if e not in my_dict:\n",
" my_dict[e] = 0\n",
" my_dict[e] += 1 \n",
"\n",
"def add_element_except(my_dict, elements):\n",
" for e in elements:\n",
" try:\n",
" my_dict[e] += 1\n",
" except KeyError:\n",
" my_dict[e] = 1\n",
" \n",
"\n",
"random.seed(123)\n",
"rand_ints = [randrange(1, 10) for i in range(100)]\n",
"empty_dict = {}\n",
"\n",
"print('Results for 100 integers in range 1-10') \n",
"%timeit add_element_check1(copy.deepcopy(empty_dict), rand_ints)\n",
"%timeit add_element_check2(copy.deepcopy(empty_dict), rand_ints)\n",
"%timeit add_element_except(copy.deepcopy(empty_dict), rand_ints)\n",
" \n",
"print('\\nResults for 1000 integers in range 1-10') \n",
"rand_ints = [randrange(1, 10) for i in range(1000)]\n",
"empty_dict = {}\n",
"\n",
"%timeit add_element_check1(copy.deepcopy(empty_dict), rand_ints)\n",
"%timeit add_element_check2(copy.deepcopy(empty_dict), rand_ints)\n",
"%timeit add_element_except(copy.deepcopy(empty_dict), rand_ints)\n",
"\n",
"print('\\nResults for 1000 integers in range 1-1000') \n",
"rand_ints = [randrange(1, 10) for i in range(1000)]\n",
"empty_dict = {}\n",
"\n",
"%timeit add_element_check1(copy.deepcopy(empty_dict), rand_ints)\n",
"%timeit add_element_check2(copy.deepcopy(empty_dict), rand_ints)\n",
"%timeit add_element_except(copy.deepcopy(empty_dict), rand_ints)\n",
"\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
"# 2.4 GHz Intel Core Duo\n",
"# 8 GB 1067 Mhz DDR3\n",
"#"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Results for 100 integers in range 1-10\n",
"10000 loops, best of 3: 30.5 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000 loops, best of 3: 32.4 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000 loops, best of 3: 31.7 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"Results for 1000 integers in range 1-10\n",
"1000 loops, best of 3: 241 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"1000 loops, best of 3: 247 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"1000 loops, best of 3: 209 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"Results for 1000 integers in range 1-1000\n",
"1000 loops, best of 3: 241 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"1000 loops, best of 3: 247 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"1000 loops, best of 3: 206 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 25
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Conclusion\n",
"Interestingly, the `try-except` loop pays off if we have more elements (here: 1000 integers instead of 100) as dictionary keys to check. Also, it doesn't matter much whether the elements exist or do not exist in the dictionary, yet."
]
},
{
"cell_type": "code",