Created
December 19, 2017 19:01
-
-
Save renzon/23ee168a76005101e2816638c5017174 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
from itertools import groupby | |
from operator import itemgetter | |
def generate_batch(generator, batch_size=10): | |
if batch_size <= 1: | |
yield from generator | |
mod_generator = list((i // batch_size, v) for i, v in enumerate(generator)) | |
for mod, tpl in groupby(mod_generator, itemgetter(0)): | |
yield (v for _, v in tpl) | |
if __name__ == '__main__': | |
for batch in generate_batch('abcdef', 2): | |
print(list(batch)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment