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

Skip Montanaro python-dev@python.org
Sat, 12 Aug 2000 11:09:53 -0700


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

Modified Files:
	tut.tex 
Log Message:
list comprehensions.  see

    http://sourceforge.net/patch/?func=detailpatch&patch_id=100654&group_id=5470

for details.



Index: tut.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/tut/tut.tex,v
retrieving revision 1.111
retrieving revision 1.112
diff -C2 -r1.111 -r1.112
*** tut.tex	2000/07/27 20:55:12	1.111
--- tut.tex	2000/08/12 18:09:50	1.112
***************
*** 1754,1757 ****
--- 1754,1778 ----
  \end{verbatim}
  
+ \subsection{List Comprehensions}
+ 
+ List comprehensions provide a concise way to create lists without resorting
+ to use of the \func{map()} or \func{filter()} 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']
+ >>> vec = [2, 4, 6]
+ >>> print [3*x for x in vec]
+ [6, 12, 18]
+ >>> vec1 = [2, 4, 6]
+ >>> vec2 = [4, 3, -9]
+ >>> print [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]
+ [6, 5, -7, 8, 7, -5, 10, 9, -3]
+ \end{verbatim}
+ 
  \section{The \keyword{del} statement \label{del}}