summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2000-08-16 21:44:03 (GMT)
committerFred Drake <fdrake@acm.org>2000-08-16 21:44:03 (GMT)
commit1aebadf0e5c8f5c7f8ff994c9ee4dba20115ceec (patch)
tree0dd068af2fa5d1c809aea4d13727c5e63c41ea93
parent2d2785aad1ad7e7ea565a58187b2d1a0efa0ea54 (diff)
downloadcpython-1aebadf0e5c8f5c7f8ff994c9ee4dba20115ceec.zip
cpython-1aebadf0e5c8f5c7f8ff994c9ee4dba20115ceec.tar.gz
cpython-1aebadf0e5c8f5c7f8ff994c9ee4dba20115ceec.tar.bz2
Ka-Ping Yee <ping@lfw.org>:
Further examples of list comprehensions.
-rw-r--r--Doc/tut/tut.tex16
1 files changed, 10 insertions, 6 deletions
diff --git a/Doc/tut/tut.tex b/Doc/tut/tut.tex
index 6133dd1..6451e2a 100644
--- a/Doc/tut/tut.tex
+++ b/Doc/tut/tut.tex
@@ -1761,17 +1761,21 @@ functions. The resulting construct tends often to be clearer than use
of those functions.
\begin{verbatim}
->>> spcs = [" Apple", " Banana ", "Coco nut "]
->>> print [s.strip() for s in spcs]
-['Apple', 'Banana', 'Coco nut']
+>>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
+>>> [weapon.strip() for weapon in freshfruit]
+['banana', 'loganberry', 'passion fruit']
>>> vec = [2, 4, 6]
->>> print [3*x for x in vec]
+>>> [3*x for x in vec]
[6, 12, 18]
+>>> [3*x for x in vec if x > 3]
+[12, 18]
+>>> [3*x for x in vec if x < 2]
+[]
>>> vec1 = [2, 4, 6]
>>> vec2 = [4, 3, -9]
->>> print [x*y for x in vec1 for y in vec2]
+>>> [x*y for x in vec1 for y in vec2]
[8, 6, -18, 16, 12, -36, 24, 18, -54]
->>> print [x+y for x in vec1 for y in vec2]
+>>> [x+y for x in vec1 for y in vec2]
[6, 5, -7, 8, 7, -5, 10, 9, -3]
\end{verbatim}