[Tutor] corresponding command in Python to "eval" in Matlab

Kent Johnson kent37 at tds.net
Sun Oct 7 22:30:30 CEST 2007


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


More information about the Tutor mailing list