Inserting elements in specific order in an array in Python

Question:

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.0and 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])]
Asked By: RFeynman123

||

Answers:

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])
Answered By: Jobel Johny
from array import *
X=array('f',[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]

Y=array('f',[])

i=0

for j in range (T2[-1]+1):
    if T2[i] == j:
        Y.append(X[i])
        i+=1
    else:
        Y.append(C1)
    

print(Y)

You don’t need to use insert, especially not repeated ones. Just assign the X values to the T2 indices in a zeros array:

In [3]: X = np.array([4.17551036e+02, 3.53856161e+02, 2.82754301e+02, 1.34119055e+02,
   ...:        6.34573886e+01, 2.08344718e+02, 1.00000000e-24])

In [4]: T2=[0, 3, 5, 8, 9, 10, 11]

In [5]: res = np.zeros(12)    
In [6]: res[T2] =X

In [7]: res
Out[7]: 
array([4.17551036e+02, 0.00000000e+00, 0.00000000e+00, 3.53856161e+02,
       0.00000000e+00, 2.82754301e+02, 0.00000000e+00, 0.00000000e+00,
       1.34119055e+02, 6.34573886e+01, 2.08344718e+02, 1.00000000e-24])

To do it with one insert, T2 has to be adjusted:

In [12]: np.insert(np.zeros(5), T2-np.arange(len(T2)), X)
Out[12]: 
array([4.17551036e+02, 0.00000000e+00, 0.00000000e+00, 3.53856161e+02,
       0.00000000e+00, 2.82754301e+02, 0.00000000e+00, 0.00000000e+00,
       1.34119055e+02, 6.34573886e+01, 2.08344718e+02, 1.00000000e-24])
Answered By: hpaulj
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.