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

Discussion in 'Python' started by noydb, Aug 18, 2011.

  1. noydb

    noydb Guest

    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!
     
    noydb, Aug 18, 2011
    #1
    1. Advertising

  2. On Thu, Aug 18, 2011 at 10:57 AM, noydb <> 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?
     
    David Robinow, Aug 18, 2011
    #2
    1. Advertising

  3. noydb

    noydb Guest

    On Aug 18, 11:12 am, David Robinow <> wrote:
    > On Thu, Aug 18, 2011 at 10:57 AM, noydb <> 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.
     
    noydb, Aug 18, 2011
    #3
  4. noydb

    Jerry Hill Guest

    On Thu, Aug 18, 2011 at 11:19 AM, noydb <> 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
     
    Jerry Hill, Aug 18, 2011
    #4
  5. noydb

    noydb Guest

    On Aug 18, 11:29 am, Jerry Hill <> wrote:
    > On Thu, Aug 18, 2011 at 11:19 AM, noydb <> 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!
     
    noydb, Aug 18, 2011
    #5
  6. noydb

    John Gordon Guest

    In <> noydb <> 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
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Aug 18, 2011
    #6
  7. On Thu, Aug 18, 2011 at 5:09 PM, John Gordon <> 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
     
    Chris Angelico, Aug 18, 2011
    #7
  8. noydb

    Nobody Guest

    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.
     
    Nobody, Aug 18, 2011
    #8
  9. noydb

    AB Guest

    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
     
    AB, Aug 18, 2011
    #9
  10. Chris Angelico wrote:

    > On Thu, Aug 18, 2011 at 5:09 PM, John Gordon <> 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
     
    Steven D'Aprano, Aug 19, 2011
    #10
  11. noydb 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]
    >
    > Thanks for any help!


    red="a string"
    one="another string"
    maple="a file path"
    old=["red", "one", "maple"]
    newList=map(eval, old)
     
    Kingsley Adio, Aug 19, 2011
    #11
  12. noydb

    Roy Smith Guest

    In article
    <>,
    noydb <> 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]
    >
    > 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"]]
     
    Roy Smith, Aug 19, 2011
    #12
  13. noydb

    noydb Guest

    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!
     
    noydb, Aug 19, 2011
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Klaus Neuner
    Replies:
    7
    Views:
    516
    Klaus Neuner
    Jul 26, 2004
  2. Harlin Seritt
    Replies:
    29
    Views:
    1,205
    Paul Rubin
    Feb 24, 2007
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    802
    Malcolm
    Jun 24, 2006
  4. Replies:
    9
    Views:
    997
  5. Wybo Dekker
    Replies:
    1
    Views:
    200
    Charles Steinman
    Jul 23, 2005
Loading...

Share This Page