Home > OS >  Inserting zeros at multiple locations of an array in Python
Inserting zeros at multiple locations of an array in Python

Time:01-01

I have a list T2 and an array X. I want to insert zeroes at specific locations of X in accordance to T2. For instance, for X[0], the zeroes have to be inserted at all locations except the ones specified in T2[0] and for X[1], the zeroes have to be inserted at all locations except the ones specified in T2[1]. I present the current and expected outputs.

import numpy as np

T2=[[0, 3, 5, 8, 9, 10, 11],[0, 2, 3, 5, 6, 8, 9, 10, 11]]

X=np.array([np.array([4.17551036e 02, 3.53856161e 02, 2.82754301e 02, 1.34119055e 02,
              6.34573886e 01, 2.08344718e 02, 1.00000000e-24])               ,
       np.array([4.17551036e 02, 3.32821605e 02, 2.94983702e 02, 2.78809292e 02,
              1.26991664e 02, 1.36026510e 02, 8.31512525e 01, 2.07329562e 02,
              1.00000000e-24])                                               ],
      dtype=object)

C1=0.0

index=0

for m in range(0,len(X)):
    for j in range(T2[m][-1]):
        if(j!=T2[m][index]):
            X[m] = np.insert(X[m], j, C1, axis=None)  
        else:
            index =1

print([X])

The current output is

[array([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]),
       array([0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00,
              0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00,
              0.00000000e 00, 4.17551036e 02, 3.32821605e 02, 2.94983702e 02,
              2.78809292e 02, 1.26991664e 02, 1.36026510e 02, 8.31512525e 01,
              2.07329562e 02, 1.00000000e-24])                               ],
      dtype=object)]

The expected output is

[array([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]),
       array([4.17551036e 02, 0.00000000e 00, 3.32821605e 02, 2.94983702e 02,
              0.00000000e 00, 2.78809292e 02, 1.26991664e 02, 0.00000000e 00,
              1.36026510e 02, 8.31512525e 01, 2.07329562e 02, 1.00000000e-24]) ],
      dtype=object)]   

CodePudding user response:

You are overcomplicating things. You can rephrase your problem as: Create an array with zeros everywhere except the indices in T2. Take those from X.

def make_array(indices, values):
    rtrn = np.zeros(np.max(indices)   1, dtype=values.dtype)
    rtrn[indices] = values
    return rtrn


X = np.array([make_array(Ti, Xi) for Ti, Xi in zip(T2, X)], dtype=object)

CodePudding user response:

You have two different tasks, the compound data structures complicate the problem, if you split the data in:

T1 = [0, 3, 5, 8, 9, 10, 11]

T2 = [0, 2, 3, 5, 6, 8, 9, 10, 11]

X1 = np.array([4.17551036e 02, 3.53856161e 02, 2.82754301e 02, 1.34119055e 02,
              6.34573886e 01, 2.08344718e 02, 1.00000000e-24])

X2 = np.array([4.17551036e 02, 3.32821605e 02, 2.94983702e 02, 2.78809292e 02,
              1.26991664e 02, 1.36026510e 02, 8.31512525e 01, 2.07329562e 02,
              1.00000000e-24])

Your two different problems can be solved with:

Y1 = np.zeros((12))
for i, value in zip(T1,X1):
    Y1[i] = value

Y2 = np.zeros((12))
for i1, i2 in enumerate(T2):
    Y2[i2] = X2[i1]
  • Related