From ae10d26696c0dbbd2d67d9065e3fa628f1e852b9 Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Sun, 11 Feb 2018 18:59:54 -0800 Subject: [PATCH] Added batch_by_property --- helpers.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/helpers.py b/helpers.py index d0e3c962..80ee9e33 100644 --- a/helpers.py +++ b/helpers.py @@ -226,6 +226,24 @@ def all_elements_are_instances(iterable, Class): def adjacent_pairs(objects): return zip(objects, list(objects[1:])+[objects[0]]) +def batch_by_property(items, property_func): + batches = [] + def add_batch(batch): + if len(batch) > 0: + batches.append(batch) + curr_batch = [] + curr_prop = None + for item in items: + prop = property_func(item) + if prop != curr_prop: + add_batch(curr_batch) + curr_prop = prop + curr_batch = [item] + else: + curr_batch.append(item) + add_batch(curr_batch) + return batches + def complex_to_R3(complex_num): return np.array((complex_num.real, complex_num.imag, 0))