[Python-checkins] python/dist/src/Doc/tut tut.tex,1.227,1.228
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Wed May 19 15:45:21 EDT 2004
Update of /cvsroot/python/python/dist/src/Doc/tut
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18118/Doc/tut
Modified Files:
tut.tex
Log Message:
Add more docs for generator expressions.
* Put in a brief, example driven tutorial entry.
* Use better examples in whatsnew24.tex.
Index: tut.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/tut/tut.tex,v
retrieving revision 1.227
retrieving revision 1.228
diff -C2 -d -r1.227 -r1.228
*** tut.tex 6 May 2004 01:35:45 -0000 1.227
--- tut.tex 19 May 2004 19:45:16 -0000 1.228
***************
*** 4400,4403 ****
--- 4400,4436 ----
more effort than writing a regular function.
+ \section{Generator Expressions\label{genexps}}
+
+ Some simple generators can be coded succinctly as expressions using a syntax
+ like list comprehensions but with parentheses instead of brackets. These
+ expressions are designed for situations where the generator is used right
+ away by an enclosing function. Generator expressions are more compact but
+ less versatile than full generator definitions and the tend to be more memory
+ friendly than equivalent list comprehensions.
+
+ Examples:
+
+ \begin{verbatim}
+ >>> sum(i*i for i in range(10)) # sum of squares
+ 285
+
+ >>> xvec = [10, 20, 30]
+ >>> yvec = [7, 5, 3]
+ >>> sum(x*y for x,y in zip(xvec, yvec)) # dot product
+ 260
+
+ >>> from math import pi, sin
+ >>> sine_table = dict((x, sin(x*pi/180)) for x in range(0, 91))
+
+ >>> unique_words = set(word for line in page for word in line.split())
+
+ >>> valedictorian = max((student.gpa, student.name) for student in graduates)
+
+ >>> data = 'golf'
+ >>> list(data[i] for i in range(len(data)-1,-1,-1))
+ ['f', 'l', 'o', 'g']
+
+ \end{verbatim}
+
More information about the Python-checkins
mailing list