I have a custom dictionary where the values are numpy arrays. I would like to have a convenience function that creates a copy of its content except that the values are changed. If no value is passed, then the default None
is assigned to the keys, else a value is set. The function can also work with a callable function, and then the resulting value is obtained by passing the length of the numpy arrays.
Inspired by what I found here, this is what I have so far:
import sys
import numpy as np
class CustomDict(dict):
def strip(self, value=None):
if callable(value):
tmp = dict.fromkeys(self.keys())
for k,v in self.items():
tmp[k] = value(len(v))
return tmp
return dict.fromkeys(self.keys(), value)
def main(argv=()):
tmp1 = CustomDict()
tmp1['n1'] = np.array([[3]])
tmp1['l2'] = np.array([[ 0, 4],
[ 4, 5],
[57, 3]])
tmp1['t3x'] = np.array([[188, 401, 400],
[188, 187, 401],
[187, 205, 401],
[324, 306, 417],
[306, 305, 417],
[305, 416, 417]])
# replace values
print(tmp1.strip(1))
# replace values with a callable function that gets the size of the array
print(tmp1.strip(lambda x: [None]*x))
return 0
if __name__ == "__main__":
sys.exit(main())
This code outputs:
{'l2': 1, 'n1': 1, 't3x': 1}
{'l2': [None, None, None], 'n1': [None], 't3x': [None, None, None, None, None, None]}
It's all good, it works. But I'm not sure the part that tackles the callable function is the most efficient one, as I am creating the copy of the dictionary first, and then I'm replacing the values with another loop. Can this be done in a single pass?