Last active
September 28, 2017 07:23
-
-
Save lihuanshuai/b6b47000f25bac7bfcc4e3ef4a1f97f6 to your computer and use it in GitHub Desktop.
Previous and next values inside a loop https://stackoverflow.com/questions/1011938/python-previous-and-next-values-inside-a-loop/1012089#1012089
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 tee, islice as slice, chain, izip as zip | |
def previous_and_next(iterable, fill_value=None): | |
prevs, items, nexts = tee(iterable, 3) | |
prevs = chain([fill_value], prevs) | |
nexts = chain(slice(nexts, 1, None), [fill_value]) | |
return zip(prevs, items, nexts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment