Skip to content

Instantly share code, notes, and snippets.

@Back2Basics
Last active May 22, 2019 07:20
Show Gist options
  • Save Back2Basics/1ef5e9ac1b436572fb954085e23fa499 to your computer and use it in GitHub Desktop.
Save Back2Basics/1ef5e9ac1b436572fb954085e23fa499 to your computer and use it in GitHub Desktop.
n_things at a time
from collections import deque, Counter
from typing import Iterable, Callable
def sliding_window(someiterable: Iterable, n: int, returnable: Callable = ''.join):
"""
generates an n element sliding window one element
at a time in whatever type you want returned
returnable could be tuple or list
"""
n_things = deque()
for thing in someiterable:
n_things.append(thing)
if len(n_things)<n:
continue
elif len(n_things)>n:
n_things.popleft()
yield returnable(n_things)
Counter(sliding_window("CGGAGGACTCTAGGTAACGC", n=3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment