Skip to content

Instantly share code, notes, and snippets.

@kzinmr
Created December 14, 2018 03:02
Show Gist options
  • Save kzinmr/6eed42b5d6eb02ee8237f1dc8a16e120 to your computer and use it in GitHub Desktop.
Save kzinmr/6eed42b5d6eb02ee8237f1dc8a16e120 to your computer and use it in GitHub Desktop.
pure python ngrams
from itertools import tee, zip_longest
def ngrams(iterable, n=3):
"""
>>> list(ngrams(range(5), 3))
[(0, 1, 2), (1, 2, 3), (2, 3, 4)]
"""
ts = tee(iterable, n)
for i, t in enumerate(ts[1:]):
for _ in range(i+1):
next(t, None)
return zip(*ts) # zip_longest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment