Commit 481a4b11 authored by Loic Huder's avatar Loic Huder
Browse files

pres07: expanded dict part and replaced subslides by slides

parent 81a47726
Pipeline #33322 passed with stage
in 58 seconds
......@@ -38,7 +38,16 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 2] [0, 1, 2] ['a', 2, 0, 1, 2]\n",
"['a', 2, 'a', 2, 'a', 2]\n"
]
}
],
"source": [
"# 2 equivalent ways to define an empty list\n",
"l0 = []\n",
......@@ -56,7 +65,7 @@
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
"slide_type": "slide"
}
},
"source": [
......@@ -67,7 +76,7 @@
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
"slide_type": "slide"
}
},
"source": [
......@@ -267,7 +276,7 @@
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
"slide_type": "slide"
}
},
"source": [
......@@ -318,7 +327,7 @@
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
"slide_type": "slide"
}
},
"source": [
......@@ -392,7 +401,7 @@
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
"slide_type": "slide"
}
},
"source": [
......@@ -513,7 +522,7 @@
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
"slide_type": "slide"
}
},
"source": [
......@@ -561,8 +570,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"integer remove from the list: 3\n",
"shuffled list: [13, 6, 8, 7, 15, 0, 14, 17, 11, 1, 18, 16, 10, 4, 2, 5, 12, 9, 19]\n"
"integer remove from the list: 11\n",
"shuffled list: [19, 15, 0, 18, 4, 2, 6, 12, 13, 8, 1, 7, 9, 3, 14, 5, 10, 16, 17]\n"
]
}
],
......@@ -594,7 +603,11 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"## A possible solution : "
]
......@@ -607,7 +620,7 @@
{
"data": {
"text/plain": [
"3"
"11"
]
},
"execution_count": 21,
......@@ -650,7 +663,11 @@
"\n",
"The dictionary (`dict`) is a very important data structure in Python. All namespaces are (nearly) dictionaries and \"Namespaces are one honking great idea -- let's do more of those!\" (The zen of Python).\n",
"\n",
"A dict is a hashtable (a set) + associated values."
"A `dict` is a hashtable (a set of keys) with associated values.\n",
"\n",
"#### Dictionary creation\n",
"Syntax: `{key1: value1, key2: value2...}`\n",
"Keys can be anything as long as they are \"hashable\" (ex: Python standard types)."
]
},
{
......@@ -662,17 +679,27 @@
"name": "stdout",
"output_type": "stream",
"text": [
"{'b': 2, 'a': 1}\n"
"{'a': 1, 'b': 2, 0: False, 1: True}\n"
]
}
],
"source": [
"d = {}\n",
"d['b'] = 2\n",
"d['a'] = 1\n",
"d = {'a': 1, 'b': 2, 0: False, 1: True}\n",
"print(d)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"#### Adding/replacing an entry to an exisiting dict: \n",
"`d[key] = value` will create a new entry or replace the value associated with `key`."
]
},
{
"cell_type": "code",
"execution_count": 23,
......@@ -682,13 +709,61 @@
"name": "stdout",
"output_type": "stream",
"text": [
"{'a': 1, 'b': 2, 0: False, 1: True}\n"
"{'Year': 2020, 'Month': 1}\n"
]
},
{
"data": {
"text/plain": [
"{'Year': 2015, 'Month': 1}"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = {'a': 1, 'b': 2, 0: False, 1: True}\n",
"print(d)"
"date = {}\n",
"date['Year'] = 2020\n",
"date['Month'] = 1\n",
"print(date)\n",
"date['Year'] = 2015\n",
"date"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"#### Get an existing entry\n",
"`d[key]` will return the value associated with `key`. If `key` does not exist, it will throw a `KeyError`."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2015\n",
"'Day' is not an existing key !\n"
]
}
],
"source": [
"print(date['Year'])\n",
"try:\n",
" current_day = date['Day']\n",
"except KeyError as e:\n",
" print(f'{e} is not an existing key !')"
]
},
{
......@@ -706,7 +781,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 25,
"metadata": {},
"outputs": [
{
......@@ -725,7 +800,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 26,
"metadata": {},
"outputs": [
{
......@@ -734,7 +809,7 @@
"'value1'"
]
},
"execution_count": 25,
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
......@@ -745,7 +820,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 27,
"metadata": {},
"outputs": [
{
......@@ -764,7 +839,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 28,
"metadata": {},
"outputs": [
{
......@@ -773,7 +848,7 @@
"'value1'"
]
},
"execution_count": 27,
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
......@@ -804,7 +879,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 29,
"metadata": {},
"outputs": [
{
......@@ -823,7 +898,7 @@
" 'values']"
]
},
"execution_count": 28,
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
......@@ -837,16 +912,16 @@
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
"slide_type": "slide"
}
},
"source": [
"### `dict`: different ways to loops over a dictionary"
"### `dict`: different ways to loop over a dictionary"
]
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 30,
"metadata": {},
"outputs": [
{
......@@ -860,14 +935,14 @@
}
],
"source": [
"# loop with items\n",
"# loop on items\n",
"for key, value in d.items():\n",
" print(key, value)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 31,
"metadata": {},
"outputs": [
{
......@@ -881,14 +956,14 @@
}
],
"source": [
"# loop with values\n",
"# loop on values\n",
"for value in d.values():\n",
" print(value)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 32,
"metadata": {},
"outputs": [
{
......@@ -902,14 +977,14 @@
}
],
"source": [
"# loop with keys\n",
"# loop on keys\n",
"for key in d.keys():\n",
" print(key)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 33,
"metadata": {},
"outputs": [
{
......@@ -932,7 +1007,7 @@
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
"slide_type": "slide"
}
},
"source": [
......@@ -943,7 +1018,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
......@@ -963,7 +1038,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 35,
"metadata": {},
"outputs": [
{
......@@ -992,7 +1067,7 @@
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
"slide_type": "slide"
}
},
"source": [
......@@ -1006,28 +1081,32 @@
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"execution_count": 36,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"len(s) = 7160000, nbkeys 33 base, count, count_count, except, collections.Counter\n",
"723 ms ± 9.23 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"750 ms ± 45 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"270 ms ± 5.72 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"702 ms ± 28.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"510 ms ± 82.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"661 ms ± 47.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"781 ms ± 88.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"793 ms ± 66.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"266 ms ± 8.45 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"668 ms ± 26.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"356 ms ± 16.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"607 ms ± 24.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"with split\n",
"len(s) = 1100000, nbkeys 90 base, count, count_count, except, collections.Counter\n",
"182 ms ± 19.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"152 ms ± 2.21 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
"1.58 s ± 64.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"130 ms ± 2.73 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
"87.6 ms ± 4.21 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
"614 ms ± 4.58 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
"165 ms ± 11.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
"175 ms ± 12.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
"1.83 s ± 81.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"130 ms ± 5 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
"85.4 ms ± 4.71 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
"747 ms ± 63.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
......@@ -1106,7 +1185,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 37,
"metadata": {},
"outputs": [
{
......@@ -1148,7 +1227,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 38,
"metadata": {},
"outputs": [
{
......@@ -1194,7 +1273,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 39,
"metadata": {},
"outputs": [
{
......@@ -1234,7 +1313,7 @@
}
],
"metadata": {
"celltoolbar": "Slideshow",
"celltoolbar": "Diaporama",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment