This can probably be cleaned up some:

    from itertools import islice
    from collections import deque

    def ngram(n, seq):
        it = iter(seq)
        d = deque(islice(it, n))
        if len(d) != n:
            return
        for s in it:
            yield tuple(d)
            d.popleft()
            d.append(s)
        if len(d) == n:
            yield tuple(d)

    def test():
        xs = range(20)
        for a in ngram(5, xs):
            print a

    test()
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to