summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-02-18 21:04:16 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-02-18 21:04:16 (GMT)
commitbb94fd829067ad6ba6e2f5a349b962cd272c7e09 (patch)
tree94adfbebfc9916d39c32b7c5cd44460b6a321c34
parent978bacdd305c1fc73ec2cc91adaeba072fe967ae (diff)
downloadcpython-bb94fd829067ad6ba6e2f5a349b962cd272c7e09.zip
cpython-bb94fd829067ad6ba6e2f5a349b962cd272c7e09.tar.gz
cpython-bb94fd829067ad6ba6e2f5a349b962cd272c7e09.tar.bz2
Generalize itertools.tee() recipe.
-rw-r--r--Doc/library/itertools.rst48
1 files changed, 24 insertions, 24 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 6e56199..a349ef9 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -112,7 +112,7 @@ loops that truncate the stream.
return
indices = 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
@@ -521,28 +521,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, once :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)
+
+ Once :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`.
.. versionadded:: 2.4
@@ -690,7 +690,7 @@ which incur interpreter overhead.
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