Home > database >  Multiple insertions in a list containing numpy arrays in Python
Multiple insertions in a list containing numpy arrays in Python

Time:07-31

I have a list Path containing two numpy arrays along with a list J and C1. I want to insert J[0] one at a time with value C1 in Path[0] and J[1] one at a time with value C1 in Path[1]. But I am running into an error with index mismatch. I present the expected output.

import numpy as np
J=[[4, 7, 10], [4, 10]]
C1=[0]


Path=[np.array([10.                 ,  0.6382821834929432 ,  0.5928417218382795 ,
        0.5542698411479658 ,  0.6677634679746701 ,  0.8578897621707481 ,
        0.6544597670890333 ,  0.32706383813570833,  0.8966468940380192 ]),
     
      np.array([10.                 ,  0.6262206291648664 ,  0.6413512609273813 ,
        0.5417310533794202 ,  0.763557281407787  ,  0.580075670670837  ,
        0.48048196888232686,  0.8537221497408958 ,  0.35651700423205657,
        0.9720842635477158 ])]


for elem in J:
    Path = [np.insert(Path, elem, C1[0])]

The error is

in <module>
    Path = [np.insert(Path, elem, C1[0])]

  File "<__array_function__ internals>", line 5, in insert

  File "C:\Users\USER\anaconda3\lib\site-packages\numpy\lib\function_base.py", line 4672, in insert
    old_mask[indices] = False

IndexError: index 8 is out of bounds for axis 0 with size 5

The expected output is

[array([10.                 ,  0.6382821834929432 ,  0.5928417218382795 ,
         0.5542698411479658 ,  0.                 ,  0.6677634679746701 ,
         0.8578897621707481 ,  0.                 ,  0.6544597670890333 ,
         0.32706383813570833,  0.                 ,  0.8966468940380192 ]),
[array([10.                 ,  0.6262206291648664 ,  0.6413512609273813 ,
         0.5417310533794202 ,  0.                 ,  0.763557281407787  ,
         0.580075670670837  ,  0.48048196888232686,  0.8537221497408958 ,
         0.35651700423205657,  0.                 ,  0.9720842635477158 ])]

CodePudding user response:

Try this:

import numpy as np
J=[[4, 7, 10], [4, 10]]
C1=[0]


Path=[np.array([10.                 ,  0.6382821834929432 ,  0.5928417218382795 ,
        0.5542698411479658 ,  0.6677634679746701 ,  0.8578897621707481 ,
        0.6544597670890333 ,  0.32706383813570833,  0.8966468940380192 ]),
     
      np.array([10.                 ,  0.6262206291648664 ,  0.6413512609273813 ,
        0.5417310533794202 ,  0.763557281407787  ,  0.580075670670837  ,
        0.48048196888232686,  0.8537221497408958 ,  0.35651700423205657,
        0.9720842635477158 ])]


for i, elem in enumerate(J):
    for ind in elem:
        Path[i] = np.insert(Path[i], ind, C1[0])

Output Path:

[array([10.        ,  0.63828218,  0.59284172,  0.55426984,  0.        ,
         0.66776347,  0.85788976,  0.        ,  0.65445977,  0.32706384,
         0.        ,  0.89664689]),
 array([10.        ,  0.62622063,  0.64135126,  0.54173105,  0.        ,
         0.76355728,  0.58007567,  0.48048197,  0.85372215,  0.356517  ,
         0.        ,  0.97208426])]

CodePudding user response:

The first problem is that your arrays have size 9 and 10, this means that the indexes in J cannot be greater than 8 and 9 respectively (remember that the first index is always 0).

Once you have fixed that, you can use:

new_path = []
for array, elem in zip(Path, J):
    new_path.append(np.insert(array, elem, C1))
  • Related