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

Jerry Hill malaclypse2 at gmail.com
Thu Aug 18 11:29:40 EDT 2011


On Thu, Aug 18, 2011 at 11:19 AM, noydb <jenn.duerr 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



More information about the Python-list mailing list