473,239 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,239 software developers and data experts.

get a list from a string

Hi,

I have a string "distances = [[1,1,1,1],[2,2,2,2]]". I want to create a
variable called distances whose value is the list [[1,1,1,1],[2,2,2,2]]. How can
I go about that? I know I can use setattr, but how do I create the list from the
string?

Regards,
Simon

Jun 7 '07 #1
5 1234
On Jun 7, 3:34 am, simon kagwe <simonka...@yahoo.comwrote:
Hi,

I have a string "distances = [[1,1,1,1],[2,2,2,2]]". I want to create a
variable called distances whose value is the list [[1,1,1,1],[2,2,2,2]]. How can
I go about that? I know I can use setattr, but how do I create the list from the
string?

Regards,
Simon
exec("distances = [[1,1,1,1],[2,2,2,2]]")

~Sean

Jun 7 '07 #2
exec("distances = [[1,1,1,1],[2,2,2,2]]")
Wow! So simple!

Thanks a lot. :-)


Jun 7 '07 #3
On Jun 7, 6:06 am, simon kagwe <simonka...@yahoo.comwrote:
exec("distances = [[1,1,1,1],[2,2,2,2]]")
To be clear, exec is *not* a function; it's a statement. That means it
can't be used in lambda functions, for example.
Wow! So simple!
but dodgy, as it'll execute any python code.
Thanks a lot. :-)
Jun 7 '07 #4
On Thu, 07 Jun 2007 11:06:54 +0000, simon kagwe wrote:
>exec("distances = [[1,1,1,1],[2,2,2,2]]")
Wow! So simple!

Thanks a lot. :-)
Yes, and when you embed this in your web-application, using data gathered
from a web-form, the black-hat hackers will thank you for the security
hole too.

Surely a much better solution would be NOT to start with a string like
"distances = [[1,1,1,1],[2,2,2,2]]" in the first place? Where does that
string come from? If it comes from the user, at run-time, using exec is a
MAJOR security hole. If it comes from the source code, then WHY???

I wish exec and eval were hidden away in a module so they were harder (but
not impossible) to get to. Because I'm paranoid, I wish importing that
module would print an warning saying "Are you MAD??? Don't do this!!!". I
wish even more that Python would come with a built-in "make a list from a
list representation" function, but that at least is fairly easy to create:
you can modify


Here is a discussion about just how hard (that is, probably impossible) it
is to make eval safe:

http://effbot.org/zone/librarybook-core-eval.htm


Jun 7 '07 #5
Ah, sorry, pre-mature sending. Stupid keyboard accelerators :(

To finish the sentence I was trying to write:

On Thu, 07 Jun 2007 22:39:41 +1000, Steven D'Aprano wrote:
I wish even more that Python would come with a built-in "make a list from a
list representation" function, but that at least is fairly easy to create:
you can modify
.... Fredrik Lundh's safe-eval function:

http://online.effbot.org/2005_11_01_archive.htm


--
Steven.

Jun 7 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Klaus Neuner | last post by:
Hello, I need a function that converts a list into a set of regexes. Like so: string_list = print string_list2regexes(string_list) This should return something like:
9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
4
by: blrmaani | last post by:
Here is what I want: string s1 = "This is a list of string"; list<string> s2 = s1.some_method(); Now, I should be able to traverse list s2 and get each member ( which is of type 'string' ). ...
13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
1
by: Little | last post by:
Could someone help me figure out how to put my project together. I can't get my mind wrapped around the creation of the 4 double Linked Lists. Thank your for your insight. 1. Create 4 double...
9
by: zacks | last post by:
I have written a serialized class that has several properties that are typed as a list of type class. When I deserialize an XML file, the list is populated just fine. But I am having trouble...
5
by: Little | last post by:
I have this program and I need to work on the test portion, which tests if a Val is in the list. It returns false no matter what could you look at the part and see what might need to be done to fix...
0
by: webmaster | last post by:
Pardon my being a total C# noob. I'm trying to take apart the dotNet Time Tracker dotNet C# starterkit sample application and replicate a part of the code. At a high level, I have a very...
6
by: Henrik Goldman | last post by:
Hello, I have a dataset which consist of a string username and string hostname as a key and then an integer representing a count as the matching "second" value in a pair. So far I've used...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.