[Tutor] a code question, but don't know question's name

Kent Johnson kent37 at tds.net
Sun Oct 7 14:48:13 CEST 2007


Happy Deer wrote:
> Dear all-
> 
> I am writing a function as follows.
> 
> def getdata(varlist):
> 
> ....
> eventually I have a variable called "data", which have exactly the same 
> number of columns as the name of variables in varlist.
> Say  varlist=['var1','var2','var3']. I want to assign var1=data[:,0], 
> var2=data[:,1], var3=data[:2] and return var1, var2, var3.
> 
> But when I write the function, I only know I want to assign data[:,0] to 
> a variable which has a name as varlist[0]. How to write this?

Generally in Python when you want to assign a value to a name determined 
by another variable, you should use a dictionary instead. The keys of 
the dict are the names and the values are the values.

Kent


More information about the Tutor mailing list