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

Alan Gauld alan.gauld at btinternet.com
Sun Oct 7 09:34:50 CEST 2007


"Happy Deer" <ihappydeer at gmail.com> wrote

> 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.

It doesn't matter what you call the variables inside the function,
that won't affect what they are called after you return the values.

The notation data[;,0] doesn't make sense and is an error in Python.
I#m not sure what you think it does. I assume you simply mean
data[0]?

Now, to your functions purpose.

If the varlist contains 7 names, do you still only want to
return var1, var2, var3? Or do you want to return all 7
values?

If so then just return data...

If not do you need to find the index of var1,var2,var3 in varlist
and return the corresponding values from data?

Its not clear to me exactly what the function is supposed to do.
And without any code it's hard to guess.

> 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?

I repeat that any variable names you define inside the function
will not be visible outside the function. If you want to pass the
name back to the outside world you should return a name/value
tuple or a dictionary eg. {'var1' : 42, 'var2': 66, 'var3' 99}

HTH


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list