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

Optimise initialisation of several np.zeros() arrays of fixed size in Python

I would like to optimise the process of initialising python np.zeros() arrays. Right now I just have a list of them like

import numpy as np

n_elements = 10

a = np.zeros(n_elements)
b = np.zeros(n_elements)
c = np.zeros(n_elements)
d = np.zeros(n_elements)

Do you know how I could make this bulk of code shorter?

I know that

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

a = b = c = d = np.zeros(n_elements)

Is not an option, since it will be all assigned to one memory slot.

Thanks

>Solution :

You can use numpy directly to generate a 2D array:

a,b,c,d = np.zeros((4, n_elements))
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