Created
June 2, 2017 02:40
-
-
Save AndyHoang/1047c1af651adee050133821c9ca6975 to your computer and use it in GitHub Desktop.
pre next from generator
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
#https://stackoverflow.com/questions/1011938/python-previous-and-next-values-inside-a-loop | |
from itertools import tee, islice, chain, izip | |
def previous_and_next(some_iterable): | |
prevs, items, nexts = tee(some_iterable, 3) | |
prevs = chain([None], prevs) | |
nexts = chain(islice(nexts, 1, None), [None]) | |
return izip(prevs, items, nexts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment