[Numpy-discussion] Variable in an array name?

josef.pktd at gmail.com josef.pktd at gmail.com
Wed Jan 12 11:22:49 EST 2011

On Wed, Jan 12, 2011 at 11:05 AM, Zachary Pincus
<zachary.pincus at yale.edu> wrote:
>> Thank you very much for the prompt response.  I have already done
>> what you
>> have suggested, but there are a few cases where I do need to have an
>> array
>> named with a variable (looping through large numbers of unrelated
>> files and
>> calculations that need to be dumped into different analyses).  It
>> would be
>> extraordinarily helpful if someone could post a solution to this
>> problem,
>> regardless of inefficiency of the method.  Thanks a ton for any
>> additional
>> help.
> You could store arrays associated with string names, or other
> identifiers, (as opposed to integer indices) in a python dict.
> Global and local namespaces are also just dicts that you can grab with
> globals() and locals(), if you really want to look up variable names
> algorithmically, but I promise you that this is really not what you
> want to be doing.

or (pretending to translate matlab)

>>> a = 5
>>> for i in range(5): exec('var_%02d = np.array([%d])'%(i, a+i))

>>> [i for i in globals() if i[:3] == 'var']
['var_00', 'var_01', 'var_02', 'var_03', 'var_04']
>>> var_00
>>> var_01

not very pythonic (?)


> Zach
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list