Created
August 16, 2016 17:02
-
-
Save adammartinez271828/67041a404dde8df501f5590684c15aef to your computer and use it in GitHub Desktop.
Sliding window operator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import tee | |
def sliding_window(iterable, size=2): | |
"""Move a sliding window across iterable | |
A sliding window is a subselection of iterable of length size. | |
Args: | |
iterable (iterable): an iterable | |
size (int): length of window | |
Returns: | |
(generator) a generator of sliding window views | |
""" | |
panes = tee(iterable, size) | |
for pane_no, pane in enumerate(panes): | |
for _ in range(pane_no): | |
next(pane) | |
yield from zip(*panes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment