Reputation: 85
What is the best way to go about the following?
I want to let a user input a nested list as follows:
grid = input('Enter grid')
The user will be inputting a grid such as this:
[['', 'X', 'O'], ['X', 'O', ''], ['X', '', 'X']]
The problem is I am passing this input to a function that is expecting the input to be a nested list.
When getting the input grid is going to be a string which will not work with my function. Also if I
type convert grid to a list using list()
it produces a list with every charter of grid, i.e [,[,','
.
What is a good way to take the input in the form above and turn it into a nested list to be passed to a function? I want the input value to a nested list as if it were set by the program itself.
Thanks
Upvotes: 2
Views: 835
Reputation: 3513
Actually, IMHO, you can simply use json.loads
and tell the user to provide a json, your program will be much more stable/usable (i.e. the user doesnt need to have prior knowledge of python but simply of json which is a standard format).
The json corresponding to the list in question is basically almost the same as you provide (the quote should be double-quote and None should be null).
ex
import json
json.loads('[["", "X", "O"], ["X", "O", ""], ["X", "", "X"]]')
# [[u'', u'X', u'O'], [u'X', u'O', u''], [u'X', u'', u'X']]
json.loads('[[null, 1, ""], [null, null, 0]]')
# [[None, 1, u''], [None, None, 0]]
If you know that you doesn't have crazy value (key with an '
inside (ex "Texas Hold'em") or key that contains None
(ex "DaNone")) then you could do basic preprocessing to handle those key before hand (if you don't want to force the simple quote)
for example:
def parse_input_to_list(input):
input = input.replace("'", '"').replace('None', 'null')
return json.loads(input)
parse_input_to_list("[['', 'X', 'O'], ['X', 'O', ''], ['X', '', 'X']]")
# [[u'', u'X', u'O'], [u'X', u'O', u''], [u'X', u'', u'X']] << the input you provided
parse_input_to_list("[['null', 1, ''], [None, None, 0]]")
# [[u'null', 1, u''], [None, None, 0]]
parse_input_to_list('[[null, 1, ""], [null, null, 0]]')
# [[None, 1, u''], [None, None, 0]]
If the input provided is the output of another program then json.dumps
the output of the other program for the data exchange (this is exactly for what Json is made for). and you're done.
If there is absolutly no way of using Json then you could use the ast solution of @alsetru which does work fine.
Upvotes: 0
Reputation: 369124
As inspectorG4dget commented, use ast.literal_eval
:
>>> import ast
>>> ast.literal_eval("[['', 'X', 'O'], ['X', 'O', ''], ['X', '', 'X']]")
[['', 'X', 'O'], ['X', 'O', ''], ['X', '', 'X']]
To filter out invalid input, catch SyntaxError
:
>>> ast.literal_eval("[['', 'X', 'O'], ['X', 'O', ''], ['X', ', 'X']]")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/ast.py", line 46, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
File "/usr/lib/python3.4/ast.py", line 35, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 1
[['', 'X', 'O'], ['X', 'O', ''], ['X', ', 'X']]
^
SyntaxError: invalid syntax
Upvotes: 1