mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
Issue #10029: Fix sample code in the docs for zip().
This commit is contained in:
parent
ae136da881
commit
066e7a974a
1 changed files with 12 additions and 5 deletions
|
@ -1220,11 +1220,18 @@ are always available. They are listed here in alphabetical order.
|
||||||
iterable argument, it returns an iterator of 1-tuples. With no arguments,
|
iterable argument, it returns an iterator of 1-tuples. With no arguments,
|
||||||
it returns an empty iterator. Equivalent to::
|
it returns an empty iterator. Equivalent to::
|
||||||
|
|
||||||
def zip(*iterables):
|
def zip(*iterables):
|
||||||
# zip('ABCD', 'xy') --> Ax By
|
# zip('ABCD', 'xy') --> Ax By
|
||||||
iterables = map(iter, iterables)
|
sentinel = object()
|
||||||
while iterables:
|
iterables = [iter(it) for it in iterables]
|
||||||
yield tuple(map(next, 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
|
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
|
makes possible an idiom for clustering a data series into n-length groups
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue