Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

Commit 0fd19401 authored by Franck Thollard's avatar Franck Thollard
Browse files

adding zip, itertools, better variable naming

parent 21633f74
......@@ -57,6 +57,17 @@
"print(3*l2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"The `itertools` module provide other ways of iterating over lists or set of lists (e.g. cartesian product, permutation, filter, ... ): https://docs.python.org/3/library/itertools.html"
]
},
{
"cell_type": "markdown",
"metadata": {
......@@ -133,6 +144,28 @@
"print(sorted([5, 2, 10, 0]))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(1, 'a'), (2, 'b'), (3, 'c')]\n",
"[('a', 'd'), ('b', 'e'), ('c', 'f')]\n"
]
}
],
"source": [
"# \"pasting\" two lists can be done using zip\n",
"l1 = [1, 2, 3]\n",
"s = 'abc'\n",
"print(list(zip(l1, l2)))\n",
"print(list(zip('abc', 'defg')))"
]
},
{
"cell_type": "markdown",
"metadata": {
......@@ -143,7 +176,7 @@
"source": [
"### `list`: list comprehension\n",
"\n",
"They are iterable so they are often used to make loops. We have already seen how to use the keyword `for`. For example to build a new list:"
"They are iterable so they are often used to make loops. We have already seen how to use the keyword `for`. For example to build a new list (side note: `x**2` computes `x^2`):"
]
},
{
......@@ -214,6 +247,27 @@
"[s for s in ['a', 'bbb', 'e'] if len(s) == 1]"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# lists comprehensions can be cascaded\n",
"[(x,y) for x in [1,2] for y in ['a','b'] ]"
]
},
{
"cell_type": "markdown",
"metadata": {
......@@ -831,7 +885,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 10,
"metadata": {},
"outputs": [
{
......@@ -864,7 +918,7 @@
}
},
"source": [
"## Do it yourself (advanced)\n",
"## Do it yourself : smart completion (advanced)\n",
"\n",
"We will reuse our function `extract_patterns`.\n",
"\n",
......@@ -929,7 +983,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 11,
"metadata": {},
"outputs": [
{
......@@ -953,12 +1007,12 @@
"def guess(prefix, count):\n",
" \"complete the prefix with the most probable pattern (according to count)\"\n",
" # get all the pattern in keys of count that starts with prefix\n",
" compat_prefix = [x for x in count.keys() if x.startswith(prefix)]\n",
" if len(compat_prefix) == 0:\n",
" compatibles_prefixes = [x for x in count.keys() if x.startswith(prefix)]\n",
" if len(compatibles_prefixes) == 0:\n",
" return None\n",
" best_prefix = compat_prefix[0]\n",
" best_prefix = compatibles_prefixes[0]\n",
" best_score = count[best_prefix]\n",
" for pref in compat_prefix[1:]:\n",
" for pref in compatibles_prefixes[1:]:\n",
" if best_score < count[best_prefix]:\n",
" best_score = count[pref]\n",
" best_prefix = pref\n",
......@@ -1031,7 +1085,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
"version": "3.7.2"
}
},
"nbformat": 4,
......
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