mirror of
https://github.com/rasbt/python_reference.git
synced 2025-01-31 13:43:48 +00:00
looping pitfall
This commit is contained in:
parent
503b1405ab
commit
b065840e59
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"name": "",
|
"name": "",
|
||||||
"signature": "sha256:be423e458dd8b1ac6a36f49330311cc1a1741127c5610a636969efddf6d91597"
|
"signature": "sha256:02c6c63beb1de9373d69615a4ba37640a7b01c8f2d088dbfaa84bdaf3452f1c5"
|
||||||
},
|
},
|
||||||
"nbformat": 3,
|
"nbformat": 3,
|
||||||
"nbformat_minor": 0,
|
"nbformat_minor": 0,
|
||||||
|
@ -63,7 +63,8 @@
|
||||||
"- [Python's LEGB scope resolution and the keywords `global` and `nonlocal`](#python_legb)\n",
|
"- [Python's LEGB scope resolution and the keywords `global` and `nonlocal`](#python_legb)\n",
|
||||||
"- [When mutable contents of immutable tuples aren't so mutable](#immutable_tuple)\n",
|
"- [When mutable contents of immutable tuples aren't so mutable](#immutable_tuple)\n",
|
||||||
"- [List comprehensions are fast, but generators are faster!?](#list_generator)\n",
|
"- [List comprehensions are fast, but generators are faster!?](#list_generator)\n",
|
||||||
"- [Public vs. private class methods and name mangling](#private_class)"
|
"- [Public vs. private class methods and name mangling](#private_class)\n",
|
||||||
|
"- [The consequences of modifying a list when looping through it](#looping_pitfall)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1299,6 +1300,23 @@
|
||||||
],
|
],
|
||||||
"prompt_number": 28
|
"prompt_number": 28
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<br>\n",
|
||||||
|
"<br>\n",
|
||||||
|
"<a name='looping_pitfall'></a>\n",
|
||||||
|
"## The consequences of modifying a list when looping through it"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"It can be really dangerous to modify a list when iterating through - it is a very common pitfall that can cause unintended behavour!"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"collapsed": false,
|
"collapsed": false,
|
||||||
|
@ -1320,7 +1338,7 @@
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"prompt_number": 1
|
"prompt_number": 3
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
@ -1343,17 +1361,27 @@
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"prompt_number": 7
|
"prompt_number": 4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<br>\n",
|
||||||
|
"<br>\n",
|
||||||
|
"**The solution** is that we are iterating through the list index by index, and if we remove one of the items in-between, we inevitably mess around with the indexing, look at the following example, and it will become clear:"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"collapsed": false,
|
"collapsed": false,
|
||||||
"input": [
|
"input": [
|
||||||
"c = [2, 4, 5, 6]\n",
|
"b = [2, 4, 5, 6]\n",
|
||||||
"for i in c:\n",
|
"for index, item in enumerate(b):\n",
|
||||||
" if i % 2 != 0:\n",
|
" print(index, item)\n",
|
||||||
" c.remove(i)\n",
|
" if not item % 2:\n",
|
||||||
"print(c)"
|
" b.remove(item)\n",
|
||||||
|
"print(b)"
|
||||||
],
|
],
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
@ -1362,39 +1390,14 @@
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"stream": "stdout",
|
"stream": "stdout",
|
||||||
"text": [
|
"text": [
|
||||||
"[2, 4, 6]\n"
|
"0 2\n",
|
||||||
|
"1 5\n",
|
||||||
|
"2 6\n",
|
||||||
|
"[4, 5]\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"prompt_number": 6
|
"prompt_number": 7
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"collapsed": false,
|
|
||||||
"input": [
|
|
||||||
"not 4 % 2"
|
|
||||||
],
|
|
||||||
"language": "python",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "pyout",
|
|
||||||
"prompt_number": 9,
|
|
||||||
"text": [
|
|
||||||
"True"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"prompt_number": 9
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"collapsed": false,
|
|
||||||
"input": [],
|
|
||||||
"language": "python",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": []
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {}
|
"metadata": {}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user