mirror of
https://github.com/rasbt/python_reference.git
synced 2025-01-18 23:37:07 +00:00
looping pitfall
This commit is contained in:
parent
503b1405ab
commit
b065840e59
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:be423e458dd8b1ac6a36f49330311cc1a1741127c5610a636969efddf6d91597"
|
||||
"signature": "sha256:02c6c63beb1de9373d69615a4ba37640a7b01c8f2d088dbfaa84bdaf3452f1c5"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
|
@ -63,7 +63,8 @@
|
|||
"- [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",
|
||||
"- [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
|
||||
},
|
||||
{
|
||||
"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",
|
||||
"collapsed": false,
|
||||
|
@ -1320,7 +1338,7 @@
|
|||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 1
|
||||
"prompt_number": 3
|
||||
},
|
||||
{
|
||||
"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",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"c = [2, 4, 5, 6]\n",
|
||||
"for i in c:\n",
|
||||
" if i % 2 != 0:\n",
|
||||
" c.remove(i)\n",
|
||||
"print(c)"
|
||||
"b = [2, 4, 5, 6]\n",
|
||||
"for index, item in enumerate(b):\n",
|
||||
" print(index, item)\n",
|
||||
" if not item % 2:\n",
|
||||
" b.remove(item)\n",
|
||||
"print(b)"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
|
@ -1362,39 +1390,14 @@
|
|||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"[2, 4, 6]\n"
|
||||
"0 2\n",
|
||||
"1 5\n",
|
||||
"2 6\n",
|
||||
"[4, 5]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 6
|
||||
},
|
||||
{
|
||||
"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": []
|
||||
"prompt_number": 7
|
||||
}
|
||||
],
|
||||
"metadata": {}
|
||||
|
|
Loading…
Reference in New Issue
Block a user