The sort of python user who regularly writes stuff like
zip(seq, islice(seq, 1, None)))
may enjoy the `itertoolz` library
https://github.com/pytoolz/toolz
which (among many other handy functions) has a sliding_window function:
def sliding_window(n, seq):
""" A sequence of overlapping subsequences
>>> list(sliding_window(2, [1, 2, 3, 4]))
[(1, 2), (2, 3), (3, 4)]
"""
return zip(*(collections.deque(itertools.islice(it, i), 0) or it
for i, it in enumerate(itertools.tee(seq, n))))
The sort of python user who regularly writes stuff like zip(seq, islice(seq, 1, None)))
may enjoy the `itertoolz` library
which (among many other handy functions) has a sliding_window function: