我需要接受以下输入并将其存储在Python中的变量中:[1, [2,3]]
我想要的结果类似于以下作业:
l = [1, [2,3]]
但是,将此作为输入接受如下将其作为字符串读取:
l = input()
有没有办法接受嵌套列表作为输入,这样类型仍然是类'列'而不是类'str'?
对于几乎任何有效的python,我认为最简单的答案是
your_variable_name = your_python_object
# So with nested list:
your_variable_name = your_nested_list
在函数中,您可以像任何其他输入一样执行此操作:
def nested_list_getter(nested_list):
# ... your processing of the list here
def接受(* args):
print(args)
这就是你在说什么?
尝试这样的事情
print("enter the value of i")
i = [[int(y) for y in x.split("/")] if "/" in x else int(x) for x in input().split(",") ]
print(i)
输出:
enter the value of i
1,2,3/4/5,6
[1, 2, [3,4,5],6]