Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiple insertions in a list containing numpy arrays in Python

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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 ])]

>Solution :

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])]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading