mirror of https://github.com/python/cpython
Add more docs for generator expressions.
* Put in a brief, example driven tutorial entry. * Use better examples in whatsnew24.tex.
This commit is contained in:
parent
ba91b9fdda
commit
170a62221c
|
@ -4399,6 +4399,39 @@ generators terminate, they automatically raise \exception{StopIteration}.
|
||||||
In combination, these features make it easy to create iterators with no
|
In combination, these features make it easy to create iterators with no
|
||||||
more effort than writing a regular function.
|
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}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
\chapter{Brief Tour of the Standard Library \label{briefTour}}
|
\chapter{Brief Tour of the Standard Library \label{briefTour}}
|
||||||
|
|
|
@ -125,25 +125,28 @@ Generator expressions are intended to be used inside functions
|
||||||
such as \function{sum()}, \function{min()}, \function{set()}, and
|
such as \function{sum()}, \function{min()}, \function{set()}, and
|
||||||
\function{dict()}. These functions consume their data all at once
|
\function{dict()}. These functions consume their data all at once
|
||||||
and would not benefit from having a full list instead of a generator
|
and would not benefit from having a full list instead of a generator
|
||||||
an input:
|
as an input:
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> sum(i*i for i in range(10))
|
>>> sum(i*i for i in range(10))
|
||||||
285
|
285
|
||||||
|
|
||||||
>>> sorted(set(i*i for i in xrange(-10, 11)))
|
>>> sorted(set(i*i for i in xrange(-20, 20) if i%2==1)) # odd squares
|
||||||
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
|
[1, 9, 25, 49, 81, 121, 169, 225, 289, 361]
|
||||||
|
|
||||||
>>> words = "Adam apple baker Bill Nancy NASA nut".split()
|
|
||||||
>>> dict((word.lower(), word) for word in words)
|
|
||||||
{'apple': 'apple', 'baker': 'baker', 'bill': 'Bill', 'nasa': 'NASA',
|
|
||||||
'adam': 'Adam', 'nancy': 'Nancy', 'nut': 'nut'}
|
|
||||||
|
|
||||||
|
>>> from itertools import izip
|
||||||
>>> xvec = [10, 20, 30]
|
>>> xvec = [10, 20, 30]
|
||||||
>>> yvec = [7, 5, 3]
|
>>> yvec = [7, 5, 3]
|
||||||
>>> sum(x*y for x,y in itertools.izip(xvec, yvec)) # dot product
|
>>> sum(x*y for x,y in izip(xvec, yvec)) # dot product
|
||||||
260
|
260
|
||||||
|
|
||||||
|
>>> from math import pi, sin
|
||||||
|
>>> sine_table = dict((x, sin(x*pi/180)) for x in xrange(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)
|
||||||
|
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
These examples show the intended use for generator expressions
|
These examples show the intended use for generator expressions
|
||||||
|
|
Loading…
Reference in New Issue