summaryrefslogtreecommitdiffstats
path: root/Doc/whatsnew
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2004-05-19 19:45:19 (GMT)
committerRaymond Hettinger <python@rcn.com>2004-05-19 19:45:19 (GMT)
commit170a62221cfdff24b52cba5fc71e59342f3b4c61 (patch)
treea4fc9020007e79324343907c201409229552fd43 /Doc/whatsnew
parentba91b9fdda5e3ecc4af447a53ee11789eca9e16b (diff)
downloadcpython-170a62221cfdff24b52cba5fc71e59342f3b4c61.zip
cpython-170a62221cfdff24b52cba5fc71e59342f3b4c61.tar.gz
cpython-170a62221cfdff24b52cba5fc71e59342f3b4c61.tar.bz2
Add more docs for generator expressions.
* Put in a brief, example driven tutorial entry. * Use better examples in whatsnew24.tex.
Diffstat (limited to 'Doc/whatsnew')
-rw-r--r--Doc/whatsnew/whatsnew24.tex21
1 files changed, 12 insertions, 9 deletions
diff --git a/Doc/whatsnew/whatsnew24.tex b/Doc/whatsnew/whatsnew24.tex
index c60f28a..b25caea 100644
--- a/Doc/whatsnew/whatsnew24.tex
+++ b/Doc/whatsnew/whatsnew24.tex
@@ -125,25 +125,28 @@ Generator expressions are intended to be used inside functions
such as \function{sum()}, \function{min()}, \function{set()}, and
\function{dict()}. These functions consume their data all at once
and would not benefit from having a full list instead of a generator
-an input:
+as an input:
\begin{verbatim}
>>> sum(i*i for i in range(10))
285
->>> sorted(set(i*i for i in xrange(-10, 11)))
-[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
-
->>> 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'}
+>>> sorted(set(i*i for i in xrange(-20, 20) if i%2==1)) # odd squares
+[1, 9, 25, 49, 81, 121, 169, 225, 289, 361]
+>>> from itertools import izip
>>> xvec = [10, 20, 30]
>>> 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
+>>> 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}
These examples show the intended use for generator expressions