Skip to content

Commit 83d953d

Browse files
committed
Replace the window() example with pairwise() which demonstrates tee().
1 parent ac1ede9 commit 83d953d

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

Doc/lib/libitertools.tex

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -405,15 +405,9 @@ \subsection{Examples \label{itertools-example}}
405405
else:
406406
return starmap(func, repeat(args, times))
407407
408-
def window(seq, n=2):
409-
"Returns a sliding window (of width n) over data from the iterable"
410-
" s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
411-
it = iter(seq)
412-
result = tuple(islice(it, n))
413-
if len(result) == n:
414-
yield result
415-
for elem in it:
416-
result = result[1:] + (elem,)
417-
yield result
408+
def pairwise(iterable):
409+
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
410+
a, b = tee(iterable)
411+
return izip(a, islice(b, 1, None))
418412
419413
\end{verbatim}

Lib/test/test_itertools.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -623,16 +623,10 @@ def f(t):
623623
... else:
624624
... return starmap(func, repeat(args, times))
625625
626-
>>> def window(seq, n=2):
627-
... "Returns a sliding window (of width n) over data from the iterable"
628-
... " s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
629-
... it = iter(seq)
630-
... result = tuple(islice(it, n))
631-
... if len(result) == n:
632-
... yield result
633-
... for elem in it:
634-
... result = result[1:] + (elem,)
635-
... yield result
626+
>>> def pairwise(iterable):
627+
... "s -> (s0,s1), (s1,s2), (s2, s3), ..."
628+
... a, b = tee(iterable)
629+
... return izip(a, islice(b, 1, None))
636630
637631
This is not part of the examples but it tests to make sure the definitions
638632
perform as purported.
@@ -681,10 +675,13 @@ def f(t):
681675
>>> take(5, imap(int, repeatfunc(random.random)))
682676
[0, 0, 0, 0, 0]
683677
684-
>>> list(window('abc'))
685-
[('a', 'b'), ('b', 'c')]
678+
>>> list(pairwise('abcd'))
679+
[('a', 'b'), ('b', 'c'), ('c', 'd')]
686680
687-
>>> list(window('abc',5))
681+
>>> list(pairwise([]))
682+
[]
683+
684+
>>> list(pairwise('a'))
688685
[]
689686
690687
>>> list(islice(padnone('abc'), 0, 6))

0 commit comments

Comments
 (0)