Last active
January 12, 2022 22:43
-
-
Save apallin/8d388b4d2873fd0f24d5eb35198380fa to your computer and use it in GitHub Desktop.
Sliding window iterable for substring generation
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 itertools import islice | |
from itertools import tee | |
def gen_substrings(iterable, size): | |
iterables = tee(iter(iterable), size) | |
window = zip(*(islice(t, n, None) for n, t in enumerate(iterables))) | |
yield from window | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment