如何从列表输出中的元组中删除 space?
How to remove space from tuple inside a list output?
我有这个输入 P (0, 2,+1) (2, 0, -1) (0, 4,+1) (4, 0, -1)
,我想以这种方式打印出来 [(0, 2, 1), (2, 0, -1), (0, 4, 1), (4, 0, -1)]
.但是由于我在输入中多了space 运行 进入这个错误。在不对输入进行任何更改的情况下,我想知道是否有人可以提供建议?谢谢
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)
我不确定上下文等。我知道这很丑陋,但在这种情况下,您可以添加一个拆分字符,以便有一个很好的简单字符来拆分。我说分裂了吗?
这是我所做的。将右括号替换为右括号和“$”,然后拆分为“$”。
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)
这给了我你想要的输出。
= RESTART: C:/Users/Jeremy Wright/Desktop/Desktop Mess/Not Work/Whosebug 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)]
>>>
我有这个输入 P (0, 2,+1) (2, 0, -1) (0, 4,+1) (4, 0, -1)
,我想以这种方式打印出来 [(0, 2, 1), (2, 0, -1), (0, 4, 1), (4, 0, -1)]
.但是由于我在输入中多了space 运行 进入这个错误。在不对输入进行任何更改的情况下,我想知道是否有人可以提供建议?谢谢
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)
我不确定上下文等。我知道这很丑陋,但在这种情况下,您可以添加一个拆分字符,以便有一个很好的简单字符来拆分。我说分裂了吗?
这是我所做的。将右括号替换为右括号和“$”,然后拆分为“$”。
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)
这给了我你想要的输出。
= RESTART: C:/Users/Jeremy Wright/Desktop/Desktop Mess/Not Work/Whosebug 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)]
>>>