Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1aebadf

Browse files
committed
Ka-Ping Yee <[email protected]>:
Further examples of list comprehensions.
1 parent 2d2785a commit 1aebadf

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

Doc/tut/tut.tex

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,17 +1761,21 @@ \subsection{List Comprehensions}
17611761
of those functions.
17621762

17631763
\begin{verbatim}
1764-
>>> spcs = [" Apple", " Banana ", "Coco nut "]
1765-
>>> print [s.strip() for s in spcs]
1766-
['Apple', 'Banana', 'Coco nut']
1764+
>>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
1765+
>>> [weapon.strip() for weapon in freshfruit]
1766+
['banana', 'loganberry', 'passion fruit']
17671767
>>> vec = [2, 4, 6]
1768-
>>> print [3*x for x in vec]
1768+
>>> [3*x for x in vec]
17691769
[6, 12, 18]
1770+
>>> [3*x for x in vec if x > 3]
1771+
[12, 18]
1772+
>>> [3*x for x in vec if x < 2]
1773+
[]
17701774
>>> vec1 = [2, 4, 6]
17711775
>>> vec2 = [4, 3, -9]
1772-
>>> print [x*y for x in vec1 for y in vec2]
1776+
>>> [x*y for x in vec1 for y in vec2]
17731777
[8, 6, -18, 16, 12, -36, 24, 18, -54]
1774-
>>> print [x+y for x in vec1 for y in vec2]
1778+
>>> [x+y for x in vec1 for y in vec2]
17751779
[6, 5, -7, 8, 7, -5, 10, 9, -3]
17761780
\end{verbatim}
17771781

0 commit comments

Comments
 (0)