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


Groups > comp.lang.python > #11798

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!news.ecp.fr!feeder1.cambriumusenet.nl!feed.tweaknews.nl!193.201.147.84.MISMATCH!xlned.com!feeder1.xlned.com!zen.net.uk!hamilton.zen.co.uk!reader02.news.zen.co.uk.POSTED!not-for-mail
From Nobody <nobody@nowhere.com>
Subject Re: How to convert a list of strings into a list of variables
Date Thu, 18 Aug 2011 19:45:28 +0100
User-Agent Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.)
Message-Id <pan.2011.08.18.18.45.27.692000@nowhere.com>
Newsgroups comp.lang.python
References <2ab25f69-6017-42a6-a7ef-c71bc2ee8547@l2g2000vbn.googlegroups.com> <j2jdg7$ie2$1@reader1.panix.com>
MIME-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
Lines 18
Organization Zen Internet
NNTP-Posting-Host d5a84efb.news.zen.co.uk
X-Trace DXC=\hEkUF[OMl>@IAOHEjT`\1YjZGX^207P;`<MJMoM3PF=A3l:WQVhb4>NQ<`Sf8^Ta?O[GACV=\_?:fb[477J_\X2
X-Complaints-To abuse@zen.co.uk
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:11798

Show key headers only | View raw


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.

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