Last active
May 22, 2019 07:20
-
-
Save Back2Basics/1ef5e9ac1b436572fb954085e23fa499 to your computer and use it in GitHub Desktop.
n_things at a time
This file contains 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 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