Skip to content

Instantly share code, notes, and snippets.

@adammartinez271828
Created August 16, 2016 17:02
Show Gist options
  • Save adammartinez271828/67041a404dde8df501f5590684c15aef to your computer and use it in GitHub Desktop.
Save adammartinez271828/67041a404dde8df501f5590684c15aef to your computer and use it in GitHub Desktop.
Sliding window operator
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