[Numpy-discussion] Variable in an array name?

Zachary Pincus zachary.pincus at yale.edu
Wed Jan 12 10:34:31 EST 2011


> Is it possible to use a variable in an array name?  I am looping  
> through a
> bunch of calculations, and need to have each array as a separate  
> entity.
> I'm pretty new to python and numpy, so forgive my ignorance.  I'm  
> sure there
> is a simple answer, but I can't seem to find it.
>
> let's say i have a variable 'i':
>
> i = 5
>
> I would like my array to have the name array5
>
> I know how I could do this manually, but not in a loop where i is  
> redefined
> several times.

There are ways to do this, but what you likely actually want is just  
to put several arrays in a python list and then index into the list,  
instead of constructing numbered names.

e.g.:

array_list = []

for whatever:
   array_list.append(numpy.array(whatever))

for array in array_list:
   do_something(array)

given_array = array_list[i]



More information about the NumPy-Discussion mailing list