I want to update ‘array’ inside the dictionary after doing interpolation.
for example, “array_ex” is a dictionary and has values like below and ‘0_array’ has (6,100) shape while ‘1_array’ has (6,200) shape…
JavaScript
x
11
11
1
{"0_array": array([[17., 20., 15., , 42., 52., 32.],
2
[24., 33., 19., , 100., 120., 90.],
3
,
4
[2., 3., 4., , 1., 3., 4.],
5
[10., 11., 12., , 13., 16., 17.]]),
6
"1_array": array([[20., 20., 15., , 42., 43., 35.],
7
[52., 33., 22., , 88., 86., 90.],
8
,
9
[10., 11., 17., , 71., 23., 24.],
10
[34., 44., 28., , 42., 43., 17.]])}
11
I wrote a function for interpolating the array using np.interp. The function interpolates the shape of array (6,100) to (6,200). However, how can I update my array after interpolating? The name of function is “convert_array”.
JavaScript
1
4
1
for i,a in array_ex.items():
2
for row in a:
3
convert_array(row,100,200) #interpolate 100 to 200
4
I want to get “array_ex” with updated array after interpolation. Thanks.
Advertisement
Answer
Start with a dict containing arrays:
JavaScript
1
2
1
In [168]: dict1 = {'one':np.arange(3), 'two':np.ones(3)}
2
Make a new dict from those arrays, same keys, new arrays:
JavaScript
1
8
1
In [169]: dict2 = {k:v*2 for k,v in dict1.items()}
2
3
In [170]: dict1
4
Out[170]: {'one': array([0, 1, 2]), 'two': array([1., 1., 1.])}
5
6
In [171]: dict2
7
Out[171]: {'one': array([0, 2, 4]), 'two': array([2., 2., 2.])}
8
Or with a loop more like yours:
JavaScript
1
7
1
In [172]: dict3 = {}
2
for k,v in dict1.items(): :
3
dict3[k] = v.repeat(2) :
4
:
5
In [173]: dict3
6
Out[173]: {'one': array([0, 0, 1, 1, 2, 2]), 'two': array([1., 1., 1., 1., 1., 1.])}
7