Added dictionary filter function
This commit is contained in:
parent
dbee6ffaa1
commit
f7c6596bef
1 changed files with 12 additions and 1 deletions
|
|
@ -1,4 +1,6 @@
|
|||
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"""
|
||||
|
|
@ -15,4 +17,13 @@ def axes_to_array(coordinate_dictionary, axis_keys=['x', 'y', 'z'], base_array=N
|
|||
if key in coordinate_dictionary:
|
||||
base_array[axis] = coordinate_dictionary[key]
|
||||
|
||||
return base_array
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue