I have this input P (0, 2, 1) (2, 0, -1) (0, 4, 1) (4, 0, -1)
and I would like to have it printed out this way [(0, 2, 1), (2, 0, -1), (0, 4, 1), (4, 0, -1)]
. However, due to the extra space in the input I ran into this error. Without making any change to the input, I wonder if anyone could advise? Thanks
algorithm_type_2 = list(eval(user_input_2))
File "<string>", line 1
(0,,2, 1),(2,,0,,-1),(0,,4, 1),(4,,0,,-1)
^
SyntaxError: invalid syntax
user_input = input().split()
# input_list = user_input.split()
# algorithm_type = 'X'
algorithm_type = user_input.pop(0)
user_input_2 = ','.join(user_input)
algorithm_type_2 = list(eval(user_input_2))
print(user_input_2)
print(algorithm_type_2)
CodePudding user response:
I'm not sure the context etc.. I know this is ugly, but in situations like this you can add a split character just to have a nice easy character to split on. Did I say split?
Here is what I did. Replace the closing bracket with a closing bracket and a "$" then split on the "$".
user_input = input().replace(")",")$").split("$")
# input_list = user_input.split()
# algorithm_type = 'X'
algorithm_type = user_input.pop(0)
user_input_2 = ','.join(user_input)
algorithm_type_2 = list(eval(user_input_2))
print(user_input_2)
print(algorithm_type_2)
This gave me the output you wanted.
= RESTART: C:/Users/Jeremy Wright/Desktop/Desktop Mess/Not Work/StackOverflow Stuff/example.py
(0, 2, 1) (2, 0, -1) (0, 4, 1) (4, 0, -1)
(2, 0, -1), (0, 4, 1), (4, 0, -1),
[(2, 0, -1), (0, 4, 1), (4, 0, -1)]
>>>