11

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]

Does python have a good way to handle this?

Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
Phrixus
  • 269
  • 2
  • 5
  • 10
  • It's not clear what you're trying to do. Are you getting the input as a string using `raw_input()` or something similar? – Max Shawabkeh Apr 15 '10 at 09:51
  • raw_input. sorry. I forgot to mention that. – Phrixus Apr 15 '10 at 10:19
  • Personman's answer was the easiest solution that I could understand instantly at my python level. However, thanks to all other pro python users, too! :D Reading all kinds of different, creative answers was very interesting! You guys are awesome! – Phrixus Apr 15 '10 at 10:39

6 Answers6

35
>>> 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)]
YOU
  • 106,832
  • 29
  • 175
  • 207
23
>>> import json
>>> json.loads('[[0,0,0], [0,0,1], [1,1,0]]')
[[0, 0, 0], [0, 0, 1], [1, 1, 0]]
Marcelo Cantos
  • 167,268
  • 37
  • 309
  • 353
6

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
  • 2,238
  • 1
  • 16
  • 24
  • Thx! This was the easiest solution that I could understand instantly at my python level. Thanks to other python users, too! :D – Phrixus Apr 15 '10 at 10:38
  • 1
    @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 Apr 15 '10 at 22:03
  • @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 Apr 15 '10 at 22:13
  • 1
    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 Apr 16 '10 at 09:46
  • 1
    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 Apr 16 '10 at 10:39
2

[[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
  • 4,323
  • 6
  • 22
  • 30
0
>>> 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
  • 27,201
  • 27
  • 83
  • 126
-3
>>> 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.

ghostdog74
  • 286,686
  • 52
  • 238
  • 332