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

Appending multiple lists containing numpy array in Python

I want to append multiple lists containing a numpy array. I try with append but it doesn’t append the two lists for different t. I present the current and expected outputs.

import numpy as np
N=2
arsigma=[]
for t in range(0,2):
    sigma=0.02109*t*np.ones((2*N*(N+1), 1))
    arsigma.append(sigma)
    arsigma=list(sigma)
    print("sigma =",[sigma])

The current output is

sigma = [array([[0.],
       [0.],
       [0.],
       [0.],
       [0.],
       [0.],
       [0.],
       [0.],
       [0.],
       [0.],
       [0.],
       [0.]])]
sigma = [array([[0.02109],
       [0.02109],
       [0.02109],
       [0.02109],
       [0.02109],
       [0.02109],
       [0.02109],
       [0.02109],
       [0.02109],
       [0.02109],
       [0.02109],
       [0.02109]])]

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

sigma=[array([[0.],
           [0.],
           [0.],
           [0.],
           [0.],
           [0.],
           [0.],
           [0.],
           [0.],
           [0.],
           [0.],
           [0.]]), array([[0.02109],
           [0.02109],
           [0.02109],
           [0.02109],
           [0.02109],
           [0.02109],
           [0.02109],
           [0.02109],
           [0.02109],
           [0.02109],
           [0.02109],
           [0.02109]])]

>Solution :

Use list comprehension instead:

N = 2
arsigma = [0.02109*t*np.ones((2*N*(N+1), 1)) for t in range(2)]
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