Commit f7c6596b authored by Joel Collins's avatar Joel Collins
Browse files

Added dictionary filter function

parent dbee6ffa
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
import copy
import operator
from functools import reduce

def axes_to_array(coordinate_dictionary, axis_keys=['x', 'y', 'z'], base_array=None):
    """Takes key-value pairs of a JSON value, and maps onto an array"""
@@ -16,3 +18,12 @@ def axes_to_array(coordinate_dictionary, axis_keys=['x', 'y', 'z'], base_array=N
            base_array[axis] = coordinate_dictionary[key]

    return base_array

def filter_dict(dictionary: dict, keys: list):
	# Get value by recursively applying getitem
	val = reduce(operator.getitem, keys, dictionary)
	
	# Create new dictionary by running reduce on key, val pairs
	out = reduce(lambda x, y: {y: x}, reversed(keys), val)
	
	return out
 No newline at end of file