tags:

views:

133

answers:

6

Hi, what I'm trying to do is..

if a user types in [[0,0,0], [0,0,1], [1,1,0]] and press enter, the program should convert this string to several lists; one list holding [0][0][0], other for [0][0][1], and the last list for [1][1][0]

I thought tuple thing would work out but no luck... :(

I started python yesterday -- (I'm C / C++ guy.) and cannot use the full advantages of this language...

Does python have a good way to handle this? I need help~ :'(

+10  A: 
>>> import json
>>> json.loads('[[0,0,0], [0,0,1], [1,1,0]]')
[[0, 0, 0], [0, 0, 1], [1, 1, 0]]
Marcelo Cantos
Nice! I was looking at this on the regex approach but this is much cleaner! +1
AutomatedTester
This is only python 2.6+ without external modules
fabrizioM
Note that this parses the expression as JSON, *not* as Python literals. For lists and integers and strings this is mostly the same. For other types, like tuples, it won't work.
Thomas Wouters
@Thomas Wouters Are you the same Thomas Wouters ,who explains Python internals to googlers?
Srinivas Reddy Thatiparthy
I like your clarity of thought and expressing it.
Srinivas Reddy Thatiparthy
A: 
>>> import re    
>>> list_strs = re.findall(r'\[\d+\,\d+\,\d+\]', s)
>>> [[[int(i)] for i in l[1:-1].split(',')] for l in list_str]
Satoru.Logic
A: 
>>> string='[[0,0,0], [0,0,1], [1,1,0]]'
>>> eval(string)
[[0, 0, 0], [0, 0, 1], [1, 1, 0]]
>>> a=eval(string)
>>> a
[[0, 0, 0], [0, 0, 1], [1, 1, 0]]

before passing your string to eval(), do the necessary sanitization first.

Please don't suggest eval() without explaining how it executes completely arbitrary Python code.
Thomas Wouters
all it takes is a user to enter "exit()"...
thepandaatemyface
A: 

This is a little more flexible than Satoru's, and doesn't use any libraries. Still, it won't work with more deeply nested lists. For that, I think you would need a recursive function (or loop), or eval.

str = "[[0,0,0],[0,0,1],[1,1,0]]"
strs = str.replace('[','').split('],')
lists = [map(int, s.replace(']','').split(',')) for s in strs]

lists now contains the list of lists you want.

Personman
Thx! This was the easiest solution that I could understand instantly at my python level. Thanks to other python users, too! :D
Phrixus
@Prixius, there's a reason this solution got no up-votes. It is easily the ugliest and most perverse solution. My vote is for @S.Mark's solution.
Marcelo Cantos
@Marcelo Cantos, Thank you for your advice. Perhaps this might consider to be a bad solution if pro python users see it. However, his solution just works for my program and it's easy to explain to others. :) Most of all, I have no clue what ast or json things are... and was wondering if it's possible without importing things.. But, again thank you for your advice. I will take a look at your answers too if I understand python deeper someday!
Phrixus
That's all well and good, but you are rewarding poor design and adopting it yourself. This is neither a good way to produce good code nor to learn a new language. It is much better to simply follow the guidance of people who know what they're talking about, and let understanding come later. Wax on! Wax off! Besides, it isn't hard to figure it out: `import ast; help(ast.literal_eval)`.
Marcelo Cantos
I'll happily admit my code is ugly - but no one has proposed anything better that follows the same constraints. Perhaps it is true that importing a library is the right thing to do in general, and failing that a general recursive solution would be preferable, but maybe you really don't want to do either of those things for some obscure but good reason.One way my code could be a little less eclectic is to use a nested list comprehension instead of map... beyond that, is there a fundamentally nicer quick-and-dirty way to do it?
Personman
+9  A: 
>>> import ast
>>> ast.literal_eval('[[0,0,0], [0,0,1], [1,1,0]]')
[[0, 0, 0], [0, 0, 1], [1, 1, 0]]

For tuples

>>> ast.literal_eval('[(0,0,0), (0,0,1), (1,1,0)]')
[(0, 0, 0), (0, 0, 1), (1, 1, 0)]
S.Mark
A: 

[[int(i) for i in x.strip(" []").split(",")] for x in s.strip('[]').split("],")]

a list comprehension in a list comprehension... but that will melt your brain

thepandaatemyface
Oh my.... this will damage my brain for sure..
Phrixus