Use list name as string

MRAB google at mrabarnett.plus.com
Wed Feb 4 12:49:57 EST 2009


Vincent Davis wrote:
> I guess what I am saying is that it does not seem like I am adding
> any information that is not already there when I have to enter that
> list and list name after all they are the same.
> 
If you write:

y = x

then both x and y refer to the same list.

The actual names of the variables and functions shouldn't matter to the
outside world; the name of an output file shouldn't depend on the name
of a variable.

 > On Wed, Feb 4, 2009 at 10:18 AM, Vincent Davis
 > <vincent at vincentdavis.net <mailto:vincent at vincentdavis.net>> wrote:
 >
 >     I know nothing but that sucks. I can think of a lot of times I
 >     would like to do something similar. There really is no way to do
 >     this, itseems like there would be some simple way kind of like
 >     str(listname)but backwards or different.
 >
 >     On Wed, Feb 4, 2009 at 10:07 AM, MRAB <google at mrabarnett.plus.com
 >     <mailto:google at mrabarnett.plus.com>> wrote:
 >
 >         Vincent Davis wrote:
 >          > Sorry for not being clear I would have something like this
 >          x = [1, 2, 3,5 ,6 ,9,234]
 >          >
 >          > Then def savedata(dataname): ..........
 >          >
 >          > savedata(x)
 >          >
 >          > this would save a to a file called x.csv This is my
 >          > problem,getting the name to be x.csv which is the same as
 >          > the name of the list.
 >          >
 >          > and the data in the file would be 1,2,3,5,6,9,234 this
 >          >  parts works
 >          >
 >         The list itself doesn't have a name. You need to pass in both
 >         the name and the list:
 >
 >         def savedata(name, data): ..........
 >
 >         savedata("x", x)
 >



More information about the Python-list mailing list