I have a list T2
and an array X
containing numpy arrays of different shape. I want to rearrange values in these arrays according to T2
. For example, for X[0]
, the elements should occupy locations according to T2[0]
and 0.
should be placed for locations not mentioned. Similarly, for X[1]
, the elements should occupy locations according to T2[1]
. I present the expected output.
import numpy as np
T2 = [[0, 3, 4, 5], [1, 2, 3, 4]]
X=np.array([np.array([4.23056174e 02, 3.39165087e 02, 3.98049092e 02, 3.68757486e 02]),
np.array([4.23056174e 02, 3.48895801e 02, 3.48895801e 02, 3.92892424e 02])])
The expected output is
X=array([array([4.23056174e 02, 0, 0, 3.39165087e 02, 3.98049092e 02, 3.68757486e 02]),
array([0, 4.23056174e 02, 3.48895801e 02, 3.48895801e 02, 3.92892424e 02])])
CodePudding user response:
import numpy as np
T2 = ...
X = ...
out = []
for t, x in zip(T2, X):
temp = np.zeros(max(t) 1)
temp[t] = x
out.append(temp)
out = np.array(out, dtype=object)
out:
array([array([423.056174, 0. , 0. , 339.165087, 398.049092,
368.757486]) ,
array([ 0. , 423.056174, 348.895801, 348.895801, 392.892424])],
dtype=object)