Prev Next

And to iterate over text, providing previous, next, and current values:


def curprevnext_iters (chars):
"""Returns an iterator which provides a
(char, previous character, next character) tuple."""
cur, prev, next = itertools.tee (chars, 3)
prev = itertools.chain (iter (unichr (0)), prev)
next = itertools.chain (itertools.islice (next, 1, None),
iter (unichr (0)))
return itertools.izip (cur, prev, next)