Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #11761 > unrolled thread
| Started by | noydb <jenn.duerr@gmail.com> |
|---|---|
| First post | 2011-08-18 07:57 -0700 |
| Last post | 2011-08-19 07:32 -0700 |
| Articles | 13 — 10 participants |
Back to article view | Back to comp.lang.python
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
| From | noydb <jenn.duerr@gmail.com> |
|---|---|
| Date | 2011-08-18 07:57 -0700 |
| Subject | How to convert a list of strings into a list of variables |
| Message-ID | <2ab25f69-6017-42a6-a7ef-c71bc2ee8547@l2g2000vbn.googlegroups.com> |
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!
[toc] | [next] | [standalone]
| From | David Robinow <drobinow@gmail.com> |
|---|---|
| Date | 2011-08-18 11:12 -0400 |
| Message-ID | <mailman.167.1313680359.27778.python-list@python.org> |
| In reply to | #11761 |
On Thu, Aug 18, 2011 at 10:57 AM, noydb <jenn.duerr@gmail.com> 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?
[toc] | [prev] | [next] | [standalone]
| From | noydb <jenn.duerr@gmail.com> |
|---|---|
| Date | 2011-08-18 08:19 -0700 |
| Message-ID | <5db667e8-d8af-42ef-9098-5b299e1a81d9@y16g2000yqk.googlegroups.com> |
| In reply to | #11767 |
On Aug 18, 11:12 am, David Robinow <drobi...@gmail.com> wrote: > On Thu, Aug 18, 2011 at 10:57 AM, noydb <jenn.du...@gmail.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | Jerry Hill <malaclypse2@gmail.com> |
|---|---|
| Date | 2011-08-18 11:29 -0400 |
| Message-ID | <mailman.168.1313681381.27778.python-list@python.org> |
| In reply to | #11768 |
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
[toc] | [prev] | [next] | [standalone]
| From | noydb <jenn.duerr@gmail.com> |
|---|---|
| Date | 2011-08-18 08:54 -0700 |
| Message-ID | <17946eaa-2bc6-4c9b-8251-729072473988@w18g2000yqc.googlegroups.com> |
| In reply to | #11769 |
On Aug 18, 11:29 am, Jerry Hill <malaclyp...@gmail.com> wrote:
> On Thu, Aug 18, 2011 at 11:19 AM, noydb <jenn.du...@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!
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2011-08-18 16:09 +0000 |
| Message-ID | <j2jdg7$ie2$1@reader1.panix.com> |
| In reply to | #11761 |
In <2ab25f69-6017-42a6-a7ef-c71bc2ee8547@l2g2000vbn.googlegroups.com> noydb <jenn.duerr@gmail.com> 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
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-08-18 18:48 +0100 |
| Message-ID | <mailman.183.1313689739.27778.python-list@python.org> |
| In reply to | #11775 |
On Thu, Aug 18, 2011 at 5:09 PM, John Gordon <gordon@panix.com> 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
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-08-19 11:42 +1000 |
| Message-ID | <4e4dbf81$0$30004$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #11790 |
Chris Angelico wrote:
> On Thu, Aug 18, 2011 at 5:09 PM, John Gordon <gordon@panix.com> 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
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2011-08-18 19:45 +0100 |
| Message-ID | <pan.2011.08.18.18.45.27.692000@nowhere.com> |
| In reply to | #11775 |
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.
[toc] | [prev] | [next] | [standalone]
| From | AB <antonio.a.barbosa@gmail.com> |
|---|---|
| Date | 2011-08-18 15:05 -0700 |
| Message-ID | <2d34b748-8bcd-4411-8f81-a3e55a0ded21@glegroupsg2000goo.googlegroups.com> |
| In reply to | #11798 |
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
[toc] | [prev] | [next] | [standalone]
| From | Kingsley Adio <adiksonline@gmail.com> |
|---|---|
| Date | 2011-08-19 00:39 -0700 |
| Message-ID | <1f26ef99-857c-455e-ae5f-c3dd7c19a4d2@v9g2000pri.googlegroups.com> |
| In reply to | #11761 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2011-08-19 08:57 -0400 |
| Message-ID | <roy-80E9C7.08575419082011@news.panix.com> |
| In reply to | #11761 |
In article <2ab25f69-6017-42a6-a7ef-c71bc2ee8547@l2g2000vbn.googlegroups.com>, noydb <jenn.duerr@gmail.com> 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"]]
[toc] | [prev] | [next] | [standalone]
| From | noydb <jenn.duerr@gmail.com> |
|---|---|
| Date | 2011-08-19 07:32 -0700 |
| Message-ID | <89a096bb-4aaa-4d21-81e2-4beecc1784b9@ea4g2000vbb.googlegroups.com> |
| In reply to | #11840 |
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!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web