Created
January 9, 2016 06:29
-
-
Save un-def/aa10a82090633a351dec to your computer and use it in GitHub Desktop.
zip with trim/fill
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 zip_trim(i1, i2, trim='shortest', default=None): | |
''' trim = 'shortest' | 'longest' | 'first' | 'second' | |
''' | |
i1_iter = iter(i1) | |
i2_iter = iter(i2) | |
while True: | |
stop = False | |
try: | |
e1 = next(i1_iter) | |
except StopIteration: | |
if trim in ('shortest', 'first'): | |
raise | |
e1 = default | |
stop = True | |
try: | |
e2 = next(i2_iter) | |
except StopIteration: | |
if trim in ('shortest', 'second') or stop: | |
raise | |
e2 = default | |
yield e1, e2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment