Created
November 6, 2019 20:43
-
-
Save csarron/2afe76e3ac7f5e2c4c6b14eadb1664c3 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
import itertools | |
def grouper(iterable, n, fillvalue=None): | |
"""Collect data into fixed-length chunks or blocks""" | |
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx | |
args = [iter(iterable)] * n | |
return itertools.zip_longest(*args, fillvalue=fillvalue) | |
a=itertools.chain.from_iterable(itertools.repeat(['a', 'b', 'c'], 4)) | |
b=itertools.chain.from_iterable(itertools.repeat(['d', 'e', 'f', 'g'], 3)) | |
for pa, pb in zip(grouper(a, 2), grouper(b, 2)): | |
print(pa, pb, [str(qt) for qt in zip(pa, pb)]) | |
print('==') | |
""" | |
output: | |
('a', 'b') ('d', 'e') ["('a', 'd')", "('b', 'e')"] | |
== | |
('c', 'a') ('f', 'g') ["('c', 'f')", "('a', 'g')"] | |
== | |
('b', 'c') ('d', 'e') ["('b', 'd')", "('c', 'e')"] | |
== | |
('a', 'b') ('f', 'g') ["('a', 'f')", "('b', 'g')"] | |
== | |
('c', 'a') ('d', 'e') ["('c', 'd')", "('a', 'e')"] | |
== | |
('b', 'c') ('f', 'g') ["('b', 'f')", "('c', 'g')"] | |
== | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment