Ask Your Question
0

list building - consecutive

asked 2013-06-23 20:09:10 +0100

mresimulator gravatar image

updated 2013-06-23 20:39:07 +0100

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 21:06:45 +0100

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

answered 2013-07-16 10:49:45 +0100

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 11:06:53 +0100

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
0

answered 2013-07-16 06:41:08 +0100

jack77 gravatar image

updated 2013-07-16 16:49:46 +0100

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 10:28:44 +0100

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

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 20:09:10 +0100

Seen: 719 times

Last updated: Jul 16 '13