[Tutor] corresponding command in Python to eval in Matlab

2007-10-07 Thread Happy Deer
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 assign each column in data to each variable in varlist and get
var1=data[:,1]... vark=data[:,k]

Anyone knows how to do this?

Fangwen
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] corresponding command in Python to eval in Matlab

2007-10-07 Thread Eric Brunson

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 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 assign each column in data to each variable in varlist and 
 get var1=data[:,1]... vark=data[:,k]

 Anyone knows how to do this?

 Fangwen


 

 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] corresponding command in Python to eval in Matlab

2007-10-07 Thread Eric Brunson

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] = y for x, y in zip( varlist, data ) ]


This will accomplish what you're trying to do, but a dict really gives  
you greater functionality than assigning to local variables.

Python is *not* Matlab.  Functions that have the same name are not 
necessarily equivalent.  Plus, you should start to think in Python 
rather than thinking in Matlab and transliterating.


Eric Brunson wrote:
 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 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 assign each column in data to each variable in varlist and 
 get var1=data[:,1]... vark=data[:,k]

 Anyone knows how to do this?

 Fangwen


 

 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
   
 

 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] corresponding command in Python to eval in Matlab

2007-10-07 Thread Kent Johnson
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 assign each column in data to each variable in varlist and get 
 var1=data[:,1]... vark=data[:,k]

A better way to do this is to make a dictionary that holds the values:

values = dict(var1=data[:,1]... vark=data[:,k])

or perhaps just a list with the columns as I have already shown you in a 
separate email.

Python is not Matlab, it will serve you well in the long run to learn 
the Python way of Python instead of trying to write Matlab programs in 
Python. Python's support for lists and dictionaries is very strong and 
useful.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] corresponding command in Python to eval in Matlab

2007-10-07 Thread Kent Johnson
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] = y
 
 or, more tersely:
 
 [ locals()[x] = y for x, y in zip( varlist, data ) ]
 
 
 This will accomplish what you're trying to do, but a dict really gives  
 you greater functionality than assigning to local variables.

This will only work at global scope where locals() is globals() and 
writable. In function scope assigning to locals()[...] does not change 
the local namespace:

In [27]: def foo():
: locals()['x'] = 1
: print x
:
:
In [28]: foo()

Traceback (most recent call last):
   File ipython console, line 1, in module
   File ipython console, line 3, in foo
type 'exceptions.NameError': global name 'x' is not defined

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor