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. any thoughts/comments/suggestions are appreciated. Thanks. -DS -- View this message in context: http://old.nabble.com/Variable-in-an-array-name--tp30645276p30645276.html Sent from the Numpy-discussion mailing list archive at Nabble.com.
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]
Zachary Pincus-2 wrote:
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] _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
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. -- View this message in context: http://old.nabble.com/Variable-in-an-array-name--tp30645276p30654306.html Sent from the Numpy-discussion mailing list archive at Nabble.com.
On 12. jan. 2011, at 16.40, dstaley wrote:
Zachary Pincus-2 wrote:
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] _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
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. --
This may be obvious, but I sometimes forget myself: have you tried python dicts?
from numpy import * a = linspace(0,10) b = a.copy() d = {'array1':a, 'array2':b} for key in d: ... dosomething(d[key]) That way, you can assign a name / key for each array variable, and use this name for file names, or whatever you need names for.
Cheers Paul.
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. Zach
On Wed, Jan 12, 2011 at 11:05 AM, Zachary Pincus
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 array([5]) var_01 array([6])
not very pythonic (?) Josef
Zach _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (4)
-
dstaley
-
josef.pktd@gmail.com
-
Paul Anton Letnes
-
Zachary Pincus