Last active
February 27, 2018 10:46
-
-
Save vegarsti/908132d8e93f6bfb54e4624da217f4ce to your computer and use it in GitHub Desktop.
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
def pairwise(it): | |
iterator = iter(it) | |
try: | |
i = next(iterator) | |
except StopIteration: | |
return | |
yield | |
try: | |
j = next(iterator) | |
except StopIteration: | |
return | |
yield | |
yield(i, j) | |
for element in iterator: | |
i, j = j, element | |
yield(i, j) | |
i = pairwise([0]) | |
for z in i: | |
print(z) | |
print() | |
""" | |
""" | |
i = pairwise([0, 1]) | |
for z in i: | |
print(z) | |
print() | |
""" | |
(0, 1) | |
""" | |
i = pairwise([0, 1, 2]) | |
for z in i: | |
print(z) | |
""" | |
(0, 1) | |
(1, 2) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment