Created
January 22, 2014 19:15
-
-
Save Jonty/8565397 to your computer and use it in GitHub Desktop.
A tiny function for interleaving lists with configurable amounts from each list in each chunk.
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 interleave_chunks(lists): | |
while lists: | |
items, amount = lists.pop(0) | |
subset = items[0:amount] | |
items = items[amount:] | |
if subset: | |
for item in subset: | |
yield item | |
if items: | |
lists.append((items, amount)) | |
if not lists: | |
raise StopIteration | |
## Example usage: | |
lists = [ | |
( | |
range(1,10), # List | |
1 # Amount from list | |
), | |
(range(100,150), 2), | |
] | |
for bit in interleave_chunks(lists): | |
print bit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment