[Python-checkins] r85345 - python/branches/py3k/Doc/library/functions.rst
raymond.hettinger
python-checkins at python.org
Sun Oct 10 07:54:39 CEST 2010
Author: raymond.hettinger
Date: Sun Oct 10 07:54:39 2010
New Revision: 85345
Log:
Issue #10029: Fix sample code in the docs for zip().
Modified:
python/branches/py3k/Doc/library/functions.rst
Modified: python/branches/py3k/Doc/library/functions.rst
==============================================================================
--- python/branches/py3k/Doc/library/functions.rst (original)
+++ python/branches/py3k/Doc/library/functions.rst Sun Oct 10 07:54:39 2010
@@ -1235,11 +1235,18 @@
iterable argument, it returns an iterator of 1-tuples. With no arguments,
it returns an empty iterator. Equivalent to::
- def zip(*iterables):
- # zip('ABCD', 'xy') --> Ax By
- iterables = map(iter, iterables)
- while iterables:
- yield tuple(map(next, iterables))
+ def zip(*iterables):
+ # zip('ABCD', 'xy') --> Ax By
+ sentinel = object()
+ iterables = [iter(it) for it in iterables]
+ while iterables:
+ result = []
+ for it in iterables:
+ elem = next(it, sentinel)
+ if elem is sentinel:
+ return
+ result.append(elem)
+ yield tuple(result)
The left-to-right evaluation order of the iterables is guaranteed. This
makes possible an idiom for clustering a data series into n-length groups
More information about the Python-checkins
mailing list