How to convert a list of strings into a list of variables

N

noydb

How would you convert a list of strings into a list of variables using
the same name of the strings?

So, ["red", "one", "maple"] into [red, one, maple]

Thanks for any help!
 
D

David Robinow

How would you convert a list of strings into a list of variables using
the same name of the strings?

So, ["red", "one", "maple"] into [red, one, maple]
Why would you want to?
 
N

noydb

How would you convert a list of strings into a list of variables using
the same name of the strings?
So, ["red", "one", "maple"] into [red, one, maple]

  Why would you want to?

I am being passed the list of strings. I have variables set up
already pointing to files. I need to loop through each variable in
the list and do things to the files. The list of strings will change
each time, include up to 22 of the same strings each time.
 
J

Jerry Hill

I am being passed the list of strings.  I have variables set up
already pointing to files.  I need to loop through each variable in
the list and do things to the files.  The list of strings will change
each time, include up to 22 of the same strings each time.

If you have a mapping of strings to values, you should just go ahead
and store them in a dictionary. Then the lookup becomes simple:

def foo(list_of_strings):
mapping = {
"bar0": "/var/log/bar0.log",
"bar1": "/usr/local/bar/bar1.txt",
"bar2": "/home/joe/logs/bar2.log",
}
for item in list_of_strings:
filename = mapping[item]
do_something(filename)


(Untested)
 
N

noydb

I am being passed the list of strings.  I have variables set up
already pointing to files.  I need to loop through each variable in
the list and do things to the files.  The list of strings will change
each time, include up to 22 of the same strings each time.

If you have a mapping of strings to values, you should just go ahead
and store them in a dictionary.  Then the lookup becomes simple:

def foo(list_of_strings):
        mapping = {
                "bar0": "/var/log/bar0.log",
                "bar1": "/usr/local/bar/bar1.txt",
                "bar2": "/home/joe/logs/bar2.log",
        }
        for item in list_of_strings:
                filename = mapping[item]
                do_something(filename)

(Untested)

Thanks, implemented something along those lines, and it worked!
 
J

John Gordon

In said:
How would you convert a list of strings into a list of variables using
the same name of the strings?
So, ["red", "one", "maple"] into [red, one, maple]
Thanks for any help!

If the strings and the object names are exactly the same, you could use
eval(). (Of course this assumes the objects already exist.)

red = "this is the red object"
one = 1
maple = "this is the maple object"

list_of_strings = ["red", "one", "maple"]
list_of_variables = []

for x in list_of_strings:
list_of_variables.append(eval(x))

for y in list_of_variables:
print y
 
N

Nobody

How would you convert a list of strings into a list of variables using
the same name of the strings?
So, ["red", "one", "maple"] into [red, one, maple]

If the strings and the object names are exactly the same, you could use
eval().

Eval is overkill for variables; use globals() and/or locals().

But data which is supposed to be indexed by a variable key (i.e. a name
which is determined at run-time) should normally be put into a dictionary.
If access with fixed keys is far more common than variable keys, using an
object (with getattr/setattr for variable keys) may be preferable.
 
A

AB

Hi,

If the «variables» are named attributes you can use getattr.


#----------------
class colors:
red=1
green=2
blue=3

c=colors()

a=['red','green','blue']

for v in a:
print v,getattr(c,v)
#-----------

AB
 
S

Steven D'Aprano

Chris said:
If this really is what you need, you can simplify it by using the
globals() dictionary - it's a regular dictionary whose contents are
all the global variables in your current module. Inside a function,
use locals() instead.

You can use locals outside of a function too, because it just returns
globals().

Lookup of names in locals/globals is much safer than eval, particularly if
there is any risk that the list of names comes from an untrusted or
potentially hostile source.

list_of_strings = ['red', 'blue',
'__import__("os").system("echo I just p0wned your system")',
'green', 'yellow']

(The simplest way out of a billion to cause grief.)

Code injection attacks are the first and second most common form of security
vulnerability, ahead of even buffer overflows. Please don't add to the
list.

http://cwe.mitre.org/top25/?2011

(Oh, and if you think that protecting against code injection attacks while
still using eval or exec is simple, please step away from the keyboard.)
 
K

Kingsley Adio

noydb said:
How would you convert a list of strings into a list of variables using
the same name of the strings?

So, ["red", "one", "maple"] into [red, one, maple]

Thanks for any help!

red="a string"
one="another string"
maple="a file path"
old=["red", "one", "maple"]
newList=map(eval, old)
 
R

Roy Smith

noydb said:
How would you convert a list of strings into a list of variables using
the same name of the strings?

So, ["red", "one", "maple"] into [red, one, maple]

Thanks for any help!

I'm not sure what you're trying to do, but explore the dictionary
returned by locals(). You can do something like:

loc = locals()
[loc["red"], loc["one"], loc["maple"]]
 
N

noydb

Thanks to all for your responses! Good lessons. I implemented
something like what Jerry Hill suggested (dictionary), which works
well for my purposes. The list of strings that is being passed into
this code is also provided by something I wrote so I do trust what is
being sent. Might use what AB suggested down the line, as tool
expands. Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top