Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to convert a list of strings into a list of variables

Reply
Thread Tools

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

 
 
noydb
Guest
Posts: n/a
 
      08-18-2011
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!
 
Reply With Quote
 
 
 
 
David Robinow
Guest
Posts: n/a
 
      08-18-2011
On Thu, Aug 18, 2011 at 10:57 AM, noydb <(E-Mail Removed)> wrote:
> 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?
 
Reply With Quote
 
 
 
 
noydb
Guest
Posts: n/a
 
      08-18-2011
On Aug 18, 11:12*am, David Robinow <(E-Mail Removed)> wrote:
> On Thu, Aug 18, 2011 at 10:57 AM, noydb <(E-Mail Removed)> wrote:
> > 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.
 
Reply With Quote
 
Jerry Hill
Guest
Posts: n/a
 
      08-18-2011
On Thu, Aug 18, 2011 at 11:19 AM, noydb <(E-Mail Removed)> wrote:
> 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)

--
Jerry
 
Reply With Quote
 
noydb
Guest
Posts: n/a
 
      08-18-2011
On Aug 18, 11:29*am, Jerry Hill <(E-Mail Removed)> wrote:
> On Thu, Aug 18, 2011 at 11:19 AM, noydb <(E-Mail Removed)> wrote:
> > 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)
>
> --
> Jerry


Thanks, implemented something along those lines, and it worked!
 
Reply With Quote
 
John Gordon
Guest
Posts: n/a
 
      08-18-2011
In <(E-Mail Removed)> noydb <(E-Mail Removed)> writes:

> 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

--
John Gordon A is for Amy, who fell down the stairs
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      08-18-2011
On Thu, Aug 18, 2011 at 5:09 PM, John Gordon <(E-Mail Removed)> wrote:
> for x in list_of_strings:
> * *list_of_variables.append(eval(x))
>


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.

http://docs.python.org/library/functions.html#globals

ChrisA
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      08-18-2011
On Thu, 18 Aug 2011 16:09:43 +0000, John Gordon wrote:

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


 
Reply With Quote
 
AB
Guest
Posts: n/a
 
      08-18-2011
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
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-19-2011
Chris Angelico wrote:

> On Thu, Aug 18, 2011 at 5:09 PM, John Gordon <(E-Mail Removed)> wrote:
>> for x in list_of_strings:
>> list_of_variables.append(eval(x))
>>

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



--
Steven

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Convert to binary and convert back to strings Harlin Seritt Python 29 02-24-2007 09:33 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM



Advertisments