Add a section about nested listcomps to the tutorial.

Thanks to Ian Bruntlett and Robert Lehmann.
This commit is contained in:
Georg Brandl 2007-12-14 19:03:36 +00:00
parent 366523c667
commit adbda844d0
2 changed files with 43 additions and 0 deletions

View File

@ -28,6 +28,7 @@ docs@python.org), and we'll be glad to correct the problem.
* Aaron Brancotti
* Georg Brandl
* Keith Briggs
* Ian Bruntlett
* Lee Busby
* Lorenzo M. Catucci
* Carl Cerecke

View File

@ -265,6 +265,48 @@ to complex expressions and nested functions::
['3.1', '3.14', '3.142', '3.1416', '3.14159']
Nested List Comprehensions
--------------------------
If you've got the stomach for it, list comprehensions can be nested. They are a
powerful tool but -- like all powerful tools -- they need to be used carefully,
if at all.
Consider the following example of a 3x3 matrix held as a list containing three
lists, one list per row::
>>> mat = [
... [1, 2, 3],
... [4, 5, 6],
... [7, 8, 9],
... ]
Now, if you wanted to swap rows and columns, you could use a list
comprehension::
>>> print [[row[i] for row in mat] for i in [0, 1, 2]]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Special care has to be taken for the *nested* list comprehension:
To avoid apprehension when nesting list comprehensions, read from right to
left.
A more verbose version of this snippet shows the flow explicitly::
for i in [0, 1, 2]:
for row in mat:
print row[i],
print
In real world, you should prefer builtin functions to complex flow statements.
The :func:`zip` function would do a great job for this use case::
>>> zip(*mat)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
See :ref:`tut-unpacking-arguments` for details on the asterisk in this line.
.. _tut-del:
The :keyword:`del` statement