Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11769

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

References <2ab25f69-6017-42a6-a7ef-c71bc2ee8547@l2g2000vbn.googlegroups.com> <mailman.167.1313680359.27778.python-list@python.org> <5db667e8-d8af-42ef-9098-5b299e1a81d9@y16g2000yqk.googlegroups.com>
Date 2011-08-18 11:29 -0400
Subject Re: How to convert a list of strings into a list of variables
From Jerry Hill <malaclypse2@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.168.1313681381.27778.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to convert a list of strings into a list of variables noydb <jenn.duerr@gmail.com> - 2011-08-18 07:57 -0700
  Re: How to convert a list of strings into a list of variables David Robinow <drobinow@gmail.com> - 2011-08-18 11:12 -0400
    Re: How to convert a list of strings into a list of variables noydb <jenn.duerr@gmail.com> - 2011-08-18 08:19 -0700
      Re: How to convert a list of strings into a list of variables Jerry Hill <malaclypse2@gmail.com> - 2011-08-18 11:29 -0400
        Re: How to convert a list of strings into a list of variables noydb <jenn.duerr@gmail.com> - 2011-08-18 08:54 -0700
  Re: How to convert a list of strings into a list of variables John Gordon <gordon@panix.com> - 2011-08-18 16:09 +0000
    Re: How to convert a list of strings into a list of variables Chris Angelico <rosuav@gmail.com> - 2011-08-18 18:48 +0100
      Re: How to convert a list of strings into a list of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-19 11:42 +1000
    Re: How to convert a list of strings into a list of variables Nobody <nobody@nowhere.com> - 2011-08-18 19:45 +0100
      Re: How to convert a list of strings into a list of variables AB <antonio.a.barbosa@gmail.com> - 2011-08-18 15:05 -0700
  Re: How to convert a list of strings into a list of variables Kingsley Adio <adiksonline@gmail.com> - 2011-08-19 00:39 -0700
  Re: How to convert a list of strings into a list of variables Roy Smith <roy@panix.com> - 2011-08-19 08:57 -0400
    Re: How to convert a list of strings into a list of variables noydb <jenn.duerr@gmail.com> - 2011-08-19 07:32 -0700

csiph-web