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
cf984cee
Commit
cf984cee
authored
Feb 18, 2009
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Generalize the itertools.tee() recipe.
parent
0654ccd1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
24 deletions
+24
-24
Doc/library/itertools.rst
Doc/library/itertools.rst
+24
-24
No files found.
Doc/library/itertools.rst
View file @
cf984cee
...
...
@@ -130,7 +130,7 @@ loops that truncate the stream.
return
indices = list(range(r))
yield tuple(pool[i] for i in indices)
while
1
:
while
True
:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
...
...
@@ -178,7 +178,7 @@ loops that truncate the stream.
return
indices = [0] * r
yield tuple(pool[i] for i in indices)
while
1
:
while
True
:
for i in reversed(range(r)):
if indices[i] != n - 1:
break
...
...
@@ -501,28 +501,28 @@ loops that truncate the stream.
.. function:: tee(iterable[, n=2])
Return *n* independent iterators from a single iterable.
The case where ``n==2``
is equivalent to::
def tee(iterable):
def gen(next, data={}):
for i in count(
):
if i in data
:
yield data.pop(i)
else:
data[i] = next()
yield data[i]
it = iter(iterable
)
return (gen(it.__next__), gen(it.__next__)
)
Note, o
nce :func:`tee` has made a split, the original *iterable* should not be
used anywhere else; otherwise, the *iterable* could get advanced without
the tee
objects being informed.
Note, this member of the toolkit may require significant auxiliary storage
(depending on how much temporary data needs to be stored). In general, if one
iterator is going to use most or all of the data before the other iterator, it
is faster to use
:func:`list` instead of :func:`tee`.
Return *n* independent iterators from a single iterable.
Equivalent to::
def tee(iterable, n=2):
it = iter(iterable)
deques = [collections.deque() for i in range(n)]
def gen(mydeque
):
while True
:
if not mydeque: # when the local deque is empty
newval = next(it) # fetch a new value and
for d in deques: # load it to all the deques
d.append(newval)
yield mydeque.popleft(
)
return tuple(gen(d) for d in deques
)
O
nce :func:`tee` has made a split, the original *iterable* should not be
used anywhere else; otherwise, the *iterable* could get advanced without
the tee
objects being informed.
This itertool may require significant auxiliary storage (depending on how
much temporary data needs to be stored). In general, if one iterator uses
most or all of the data before another iterator starts, it is faster to use
:func:`list` instead of :func:`tee`.
.. function:: zip_longest(*iterables[, fillvalue])
...
...
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