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

noydb jenn.duerr at gmail.com
Thu Aug 18 11:54:48 EDT 2011


On Aug 18, 11:29 am, Jerry Hill <malaclyp... at gmail.com> wrote:
> On Thu, Aug 18, 2011 at 11:19 AM, noydb <jenn.du... at gmail.com> 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!



More information about the Python-list mailing list