Last active
August 29, 2015 14:10
-
-
Save wolfgangmeyers/e53bd796041fcd48ad3c to your computer and use it in GitHub Desktop.
python generic segmentation function to break items into sub-groups based on max size
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 segment_items(items, max_count_per_segment): | |
result = [] | |
index = 0 | |
while index < len(items): | |
segment = [] | |
segment_index = 0 | |
while segment_index < max_count_per_segment and index < len(items): | |
item = items[index] | |
segment.append(item) | |
segment_index += 1 | |
index += 1 | |
result.append(segment) | |
return result | |
def segment_items_betterer(items, max_count_per_segment): | |
indices = [i for i in range(len(items)) if i % max_count_per_segment == 0] | |
return [items[i:i + max_count_per_segment] for i in indices] | |
def segment_items_best(items, max_count_per_segment): | |
for i in (i for i in xrange(len(items)) if i % max_count_per_segment == 0): | |
yield items[i:i + max_count_per_segment] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment