Ask Your Question
0

list building - consecutive

asked 2013-06-23 13:09:10 -0500

mresimulator gravatar image

updated 2013-06-23 13:39:07 -0500

calc314 gravatar image

Hi experts!

How can i create N empty NUmpy arrays or lists (L1, L2, L3, ..., LN) in a 'for' cycle, i.e.:

for j in srange(N):
     Lj=np.empty(A)

(A= numbers of element in the array).

Waiting for your answers.

Thanks a lot.

edit retag flag offensive close merge delete

5 answers

Sort by ยป oldest newest most voted
2

answered 2013-06-23 14:06:45 -0500

Volker Braun gravatar image
sage: [np.empty(A) for j in range(N)]
edit flag offensive delete link more
1

answered 2013-07-16 03:49:45 -0500

Volker Braun gravatar image

It seems that the question is about generating new global variables in code. The correct way to do that is to add them to the globals() dictionary (has nothing to do with numpy)

sage: import numpy
sage: for i in range(1, 5):
....:     variable_name = 'L'+str(i)
....:     globals()[variable_name] = numpy.empty(3)
sage: L1
array([  1.26596386e-312,   1.26596386e-312,   6.90349880e-310])
edit flag offensive delete link more
0

answered 2013-06-24 03:28:44 -0500

mresimulator gravatar image

Hi! Thank for the answer...

Maybe i didnt express me right...

In my script i wanna write a 'for' cycle that create N arrays separated, named L1, L2, ..., LN.

I mean: how can i introduce this successives numbers (1,2,...,N) in the array (or list) names whis that cycle?

Waiting for your answers.

Thanks a lot.

edit flag offensive delete link more
0

answered 2013-07-15 23:41:08 -0500

jack77 gravatar image

updated 2013-07-16 09:49:46 -0500

Hello mresimulator,



I do not know, why you want to encode the index j in the name,
but you could use 'exec()' for this:

import numpy as np
N = 4
A = 3
for j in srange(N):
   exec('L'+str(j)+"=np.empty(A)")

Then L1 to L4 gets defined:

>>> L1
array([  9.79748720e-251,   2.90930926e-252,   2.34745402e-251])
edit flag offensive delete link more
0

answered 2013-06-24 04:06:53 -0500

tmonteil gravatar image

Actually, the answer of @volker-braun was quite satisfactory, since it creates a list that you can call:

# data you should add in your question to ease reproduction:
sage: import numpy as np
sage: N = 4
sage: A = 3

sage: L = [np.empty(A) for j in range(N)]
sage: L[0]
array([  6.89920558e-310,   6.89920558e-310,   0.00000000e+000])
sage: L[3]
array([  6.89920558e-310,   6.89920558e-310,   0.00000000e+000])

But you should notice that elements of a list L are numbered from 0 to len(L)-1.

Perhaps are you looking for a dictionary:

sage: L = dict()
sage: for j in srange(1, N+1):
sage:     L[j]=np.empty(A)
sage: L
{1: array([  6.89920558e-310,   5.92371271e-316,   5.43231038e-310]),
 2: array([  6.89920558e-310,   5.91607169e-316,   5.40520702e-316]),
 3: array([  6.89920558e-310,   5.91582347e-316,   5.43231038e-310]),
 4: array([  6.89920558e-310,   5.91594758e-316,   1.09178468e-316])}
sage: L[1]
array([  6.89920558e-310,   5.92371271e-316,   5.43231038e-310])
sage: L[4]
array([  6.89920558e-310,   5.91594758e-316,   1.09178468e-316])
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-06-23 13:09:10 -0500

Seen: 164 times

Last updated: Jul 16 '13