Eric Brunson wrote:
> However, I didn't actually answer your question.
>
> As Kent has already mentioned, eval is quite dangerous in python and to
> be avoided when possible. I think it would be safer to do something
> like this:
>
> l = locals()
> for x, y in zip( varlist, data ):
> l[x]
Happy Deer wrote:
> Dear all-
>
> I wonder whether there is a way in Python which can do what "eval" in
> Matlab does.
> Say, varlist is a 1 by k tuple/list, which contains strings for variable
> names.
> For example, varlist=['var1','var2',...'vark']
> data is a n by k matrix.
> I want to assi
However, I didn't actually answer your question.
As Kent has already mentioned, eval is quite dangerous in python and to
be avoided when possible. I think it would be safer to do something
like this:
l = locals()
for x, y in zip( varlist, data ):
l[x] = y
or, more tersely:
[ locals()[x]
A good python coder would probably not choose to pollute his name space
like that. My choice would be to assign the elements of "data" to a
dictionary indexed by the strings in varlist like this:
vardict = dict( zip( varlist, data ) )
and reference "var1" as:
vardict['var1']
Happy Deer wrot