mirror of
https://github.com/python/cpython.git
synced 2025-08-03 00:23:06 +00:00
Replace the window() example with pairwise() which demonstrates tee().
This commit is contained in:
parent
f0c5aec85f
commit
d591f666de
2 changed files with 14 additions and 23 deletions
|
@ -405,15 +405,9 @@ def repeatfunc(func, times=None, *args):
|
|||
else:
|
||||
return starmap(func, repeat(args, times))
|
||||
|
||||
def window(seq, n=2):
|
||||
"Returns a sliding window (of width n) over data from the iterable"
|
||||
" s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
|
||||
it = iter(seq)
|
||||
result = tuple(islice(it, n))
|
||||
if len(result) == n:
|
||||
yield result
|
||||
for elem in it:
|
||||
result = result[1:] + (elem,)
|
||||
yield result
|
||||
def pairwise(iterable):
|
||||
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
|
||||
a, b = tee(iterable)
|
||||
return izip(a, islice(b, 1, None))
|
||||
|
||||
\end{verbatim}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue