Advertisements
I have an array X
. I want to insert elements of X
at specific positions according to the list T2
and for all other positions 0.0
and create a new X
. For example, 4.17551036e+02
should be at X[0]
, 3.53856161e+02
at X[3]
, 2.82754301e+02
at X[5]
and so on. I present the current and expected outputs.
X = np.array([4.17551036e+02, 3.53856161e+02, 2.82754301e+02, 1.34119055e+02,
6.34573886e+01, 2.08344718e+02, 1.00000000e-24])
C1=0.0
T2=[0, 3, 5, 8, 9, 10, 11]
for i in T2:
X = np.insert(X, i, C1, axis=None)
print("X =", [X])
The current output is
X = [array([0.00000000e+00, 4.17551036e+02, 3.53856161e+02, 0.00000000e+00,
2.82754301e+02, 0.00000000e+00, 1.34119055e+02, 6.34573886e+01,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
2.08344718e+02, 1.00000000e-24])]
The expected output is
X = [array([4.17551036e+02, 0.0, 0.0, 3.53856161e+02, 0.0,
2.82754301e+02, 0.0, 0.0, 1.34119055e+02, 6.34573886e+01`,
2.08344718e+02, 1.00000000e-24])]
>Solution :
I believe what you are trying to achieve is, filling the rest of the values apart from the ones in the T2 list with 0.0’s. this might help you get started
X = np.array([4.17551036e+02, 3.53856161e+02, 2.82754301e+02,
1.34119055e+02, 6.34573886e+01, 2.08344718e+02, 1.00000000e-24])
C1=0.0
T2=[0, 3, 5, 8, 9, 10, 11]
index=0
for j in range(T2[-1]):
if(j!=T2[index]):
X = np.insert(X, j, C1, axis=None)
else:
index+=1
print("X =", [X])