[Python-checkins] CVS: python/dist/src/Doc/tut tut.tex,1.114,1.115

Skip Montanaro python-dev@python.org
Mon, 21 Aug 2000 19:43:09 -0700


Update of /cvsroot/python/python/dist/src/Doc/tut
In directory slayer.i.sourceforge.net:/tmp/cvs-serv4225/Doc/tut

Modified Files:
	tut.tex 
Log Message:
require list comprehensions to start with a for clause


Index: tut.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/tut/tut.tex,v
retrieving revision 1.114
retrieving revision 1.115
diff -C2 -r1.114 -r1.115
*** tut.tex	2000/08/16 21:44:03	1.114
--- tut.tex	2000/08/22 02:43:06	1.115
***************
*** 1756,1763 ****
  \subsection{List Comprehensions}
  
! List comprehensions provide a concise way to create lists without
! resorting to use of the \function{map()} or \function{filter()}
! functions.  The resulting construct tends often to be clearer than use
! of those functions.
  
  \begin{verbatim}
--- 1756,1768 ----
  \subsection{List Comprehensions}
  
! List comprehensions provide a concise way to create lists without resorting
! to use of \function{map()}, \function{filter()} and/or \keyword{lambda}.
! The resulting list definition tends often to be clearer than lists built
! using those constructs.  Each list comprehension consists of an expression
! following by a \keyword{for} clause, then zero or more \keyword{for} or
! \keyword{if} clauses.  The result will be a list resulting from evaluating
! the expression in the context of the \keyword{for} and \keyword{if} clauses
! which follow it.  If the expression would evaluate to a tuple, it must be
! parenthesized.
  
  \begin{verbatim}
***************
*** 1772,1775 ****
--- 1777,1791 ----
  >>> [3*x for x in vec if x < 2]
  []
+ >>> [{x: x**2} for x in vec]
+ [{2: 4}, {4: 16}, {6: 36}]
+ >>> [[x,x**2] for x in vec]
+ [[2, 4], [4, 16], [6, 36]]
+ >>> [x, x**2 for x in vec]	# error - parens required for tuples
+   File "<stdin>", line 1
+     [x, x**2 for x in vec]
+                ^
+ SyntaxError: invalid syntax
+ >>> [(x, x**2) for x in vec]
+ [(2, 4), (4, 16), (6, 36)]
  >>> vec1 = [2, 4, 6]
  >>> vec2 = [4, 3, -9]