]> granicus.if.org Git - python/commitdiff
Document itertools.product().
authorRaymond Hettinger <python@rcn.com>
Fri, 22 Feb 2008 19:50:06 +0000 (19:50 +0000)
committerRaymond Hettinger <python@rcn.com>
Fri, 22 Feb 2008 19:50:06 +0000 (19:50 +0000)
Doc/library/itertools.rst
Misc/NEWS

index d8faba7fa18b2237bea75b915193530a656d6dc5..7d1ec9664056385bd7df091a895399a7404f38b3 100644 (file)
@@ -302,6 +302,29 @@ loops that truncate the stream.
 
    .. 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])
 
index 2e63c822814cccb4438a6fa11c7456a67bebbc0d..5848dbaa5be748472cc0113719abe47b3f497b56 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -635,6 +635,9 @@ Library
 - 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).