Jav*_*ser 0 python nested list-comprehension list
我在尝试迭代python中的嵌套列表时遇到问题,并将列表中的值复制到另一个嵌套列表中,在我去的时候为每个值添加一个.
说我有一个清单
input = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Run Code Online (Sandbox Code Playgroud)
我尝试创建第二个列表(称之为output),是:
output = [[x + 1 for int(x)in y] for y in input]
Run Code Online (Sandbox Code Playgroud)
这给了我错误
SyntaxError: can't assign to function call
Run Code Online (Sandbox Code Playgroud)
编辑:
感谢答案,问题是尝试调用int(x) - 这完全没必要.此外,我对调用列表似乎没有任何问题input
你有几个问题:
input是一个内置函数,所以你不应该将它用作变量名;in在你的内部列表理解之前,有一个空间缺失; 和y依次分配每个值int(x),因此错误消息can't assign to function call.该int调用是不必要的,无论如何,作为你的价值观已经整数.
相反,尝试:
input_ = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
output = [[x + 1 for x in y] for y in input_]
Run Code Online (Sandbox Code Playgroud)