【发布时间】:2014-08-29 11:11:15
【问题描述】:
我在尝试迭代 python 中的嵌套列表时遇到问题,并将列表中的值复制到另一个嵌套列表中,同时为每个值添加一个。
假设我有一个清单
input = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
我尝试创建第二个列表(称为output)是:
output = [[x + 1 for int(x)in y] for y in input]
这给了我错误
SyntaxError: can't assign to function call
编辑:
感谢答案,问题是试图调用 int(x) - 这完全没有必要。此外,我调用列表input
【问题讨论】:
-
我想你只是在:
[[x + 1 for x in y] for y in input]- 不知道你想用你的int电话做什么 - 他们已经是ints... -
包含完整的错误回溯通常会有所帮助,而不是“根本不起作用”。
标签: python list nested list-comprehension