Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
6f45d18c
Commit
6f45d18c
authored
Oct 30, 2011
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve itertools docs with clearer examples of pure python equivalent code.
parent
e584457e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
10 deletions
+19
-10
Doc/library/functions.rst
Doc/library/functions.rst
+3
-3
Doc/library/itertools.rst
Doc/library/itertools.rst
+16
-7
No files found.
Doc/library/functions.rst
View file @
6f45d18c
...
...
@@ -1367,10 +1367,10 @@ are always available. They are listed here in alphabetical order.
def zip(*iterables):
# zip('ABCD', 'xy') --> Ax By
sentinel = object()
itera
ble
s = [iter(it) for it in iterables]
while itera
ble
s:
itera
tor
s = [iter(it) for it in iterables]
while itera
tor
s:
result = []
for it in itera
ble
s:
for it in itera
tor
s:
elem = next(it, sentinel)
if elem is sentinel:
return
...
...
Doc/library/itertools.rst
View file @
6f45d18c
...
...
@@ -557,16 +557,25 @@ loops that truncate the stream.
iterables are of uneven length, missing values are filled-in with *fillvalue*.
Iteration continues until the longest iterable is exhausted. Equivalent to::
def zip_longest(*args, fillvalue=None):
class ZipExhausted(Exception):
pass
def zip_longest(*args, **kwds):
# zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
yield counter() # yields the fillvalue, or raises IndexError
fillvalue = kwds.get('fillvalue')
counter = len(args) - 1
def sentinel():
nonlocal counter
if not counter:
raise ZipExhausted
counter -= 1
yield fillvalue
fillers = repeat(fillvalue)
iters = [chain(it, sentinel(), fillers) for it in args]
iter
ator
s = [chain(it, sentinel(), fillers) for it in args]
try:
for tup in zip(*iters)
:
yield tup
except
IndexError
:
while iterators
:
yield tup
le(map(next, iterators))
except
ZipExhausted
:
pass
If one of the iterables is potentially infinite, then the :func:`zip_longest`
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment