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

Skip to content

Commit 170a622

Browse files
committed
Add more docs for generator expressions.
* Put in a brief, example driven tutorial entry. * Use better examples in whatsnew24.tex.
1 parent ba91b9f commit 170a622

2 files changed

Lines changed: 45 additions & 9 deletions

File tree

Doc/tut/tut.tex

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4399,6 +4399,39 @@ \section{Generators\label{generators}}
43994399
In combination, these features make it easy to create iterators with no
44004400
more effort than writing a regular function.
44014401
4402+
\section{Generator Expressions\label{genexps}}
4403+
4404+
Some simple generators can be coded succinctly as expressions using a syntax
4405+
like list comprehensions but with parentheses instead of brackets. These
4406+
expressions are designed for situations where the generator is used right
4407+
away by an enclosing function. Generator expressions are more compact but
4408+
less versatile than full generator definitions and the tend to be more memory
4409+
friendly than equivalent list comprehensions.
4410+
4411+
Examples:
4412+
4413+
\begin{verbatim}
4414+
>>> sum(i*i for i in range(10)) # sum of squares
4415+
285
4416+
4417+
>>> xvec = [10, 20, 30]
4418+
>>> yvec = [7, 5, 3]
4419+
>>> sum(x*y for x,y in zip(xvec, yvec)) # dot product
4420+
260
4421+
4422+
>>> from math import pi, sin
4423+
>>> sine_table = dict((x, sin(x*pi/180)) for x in range(0, 91))
4424+
4425+
>>> unique_words = set(word for line in page for word in line.split())
4426+
4427+
>>> valedictorian = max((student.gpa, student.name) for student in graduates)
4428+
4429+
>>> data = 'golf'
4430+
>>> list(data[i] for i in range(len(data)-1,-1,-1))
4431+
['f', 'l', 'o', 'g']
4432+
4433+
\end{verbatim}
4434+
44024435
44034436
44044437
\chapter{Brief Tour of the Standard Library \label{briefTour}}

Doc/whatsnew/whatsnew24.tex

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,25 +125,28 @@ \section{PEP 229: Generator Expressions}
125125
such as \function{sum()}, \function{min()}, \function{set()}, and
126126
\function{dict()}. These functions consume their data all at once
127127
and would not benefit from having a full list instead of a generator
128-
an input:
128+
as an input:
129129

130130
\begin{verbatim}
131131
>>> sum(i*i for i in range(10))
132132
285
133133
134-
>>> sorted(set(i*i for i in xrange(-10, 11)))
135-
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
136-
137-
>>> words = "Adam apple baker Bill Nancy NASA nut".split()
138-
>>> dict((word.lower(), word) for word in words)
139-
{'apple': 'apple', 'baker': 'baker', 'bill': 'Bill', 'nasa': 'NASA',
140-
'adam': 'Adam', 'nancy': 'Nancy', 'nut': 'nut'}
134+
>>> sorted(set(i*i for i in xrange(-20, 20) if i%2==1)) # odd squares
135+
[1, 9, 25, 49, 81, 121, 169, 225, 289, 361]
141136
137+
>>> from itertools import izip
142138
>>> xvec = [10, 20, 30]
143139
>>> yvec = [7, 5, 3]
144-
>>> sum(x*y for x,y in itertools.izip(xvec, yvec)) # dot product
140+
>>> sum(x*y for x,y in izip(xvec, yvec)) # dot product
145141
260
146142
143+
>>> from math import pi, sin
144+
>>> sine_table = dict((x, sin(x*pi/180)) for x in xrange(0, 91))
145+
146+
>>> unique_words = set(word for line in page for word in line.split())
147+
148+
>>> valedictorian = max((student.gpa, student.name) for student in graduates)
149+
147150
\end{verbatim}
148151

149152
These examples show the intended use for generator expressions

0 commit comments

Comments
 (0)