[Python-checkins] r60968 - in python/trunk: Doc/library/itertools.rst Misc/NEWS
raymond.hettinger
python-checkins at python.org
Fri Feb 22 20:50:06 CET 2008
Author: raymond.hettinger
Date: Fri Feb 22 20:50:06 2008
New Revision: 60968
Modified:
python/trunk/Doc/library/itertools.rst
python/trunk/Misc/NEWS
Log:
Document itertools.product().
Modified: python/trunk/Doc/library/itertools.rst
==============================================================================
--- python/trunk/Doc/library/itertools.rst (original)
+++ python/trunk/Doc/library/itertools.rst Fri Feb 22 20:50:06 2008
@@ -302,6 +302,29 @@
.. versionadded:: 2.6
+.. function:: product(*iterables)
+
+ Cartesian product of input iterables.
+
+ Equivalent to nested for-loops in a generator expression. For example,
+ ``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``.
+
+ The leftmost iterators are in the outermost for-loop, so the output tuples
+ cycle in a manner similar to an odometer (with the rightmost element
+ changing on every iteration).
+
+ Equivalent to (but without building the entire result in memory)::
+
+ def product(*args):
+ pools = map(tuple, args)
+ if pools:
+ result = [[]]
+ for pool in pools:
+ result = [x+[y] for x in result for y in pool]
+ for prod in result:
+ yield tuple(prod)
+
+ .. versionadded:: 2.6
.. function:: repeat(object[, times])
Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS (original)
+++ python/trunk/Misc/NEWS Fri Feb 22 20:50:06 2008
@@ -635,6 +635,9 @@
- itertools.count() is no longer bounded to LONG_MAX. Formerly, it raised
an OverflowError. Now, automatically shifts from ints to longs.
+- Added itertools.product() which forms the Cartesian product of
+ the input iterables.
+
- Patch #1541463: optimize performance of cgi.FieldStorage operations.
- Decimal is fully updated to the latest Decimal Specification (v1.66).
More information about the Python-checkins
mailing list