Created
July 27, 2016 14:31
-
-
Save scuerda/ded304c86653788a2a942d749c6b657b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Simple python function to flatten a dict with a regular structure into a list of dicts | |
# This is essentially the inverse of a d3 roll-up, where we know that the deepest leaves will contain all necessary keys | |
def rollout(d, current_level=0, depth_to_flatten=0): | |
items = [] | |
for k, v in d.items(): | |
if current_level == depth_to_flatten: | |
if v == {}: | |
pass | |
else: | |
items.append(v) | |
else: | |
l = current_level + 1 | |
items.extend(flatten_to_depth(v, l, depth_to_flatten)) | |
return items |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment