【发布时间】:2018-01-30 16:13:28
【问题描述】:
我看到this 使用res = [*a, *b] 合并列表的解决方案,例如a = [1,2,3], b = [4,5,6]。
假设我有一个子列表列表,例如 ls = [a,b],是否可以执行 res = [*i for i in ls] 之类的操作?
自 SyntaxError: iterable unpacking cannot be used in comprehension 以来,该特定行无效。可以做类似的事情吗?
如果没有,如何轻松创建包含子列表中所有元素的列表?
使用 python 3.5.3
【问题讨论】:
-
该语法适用于 python3.6
-
@cᴏʟᴅsᴘᴇᴇᴅ 使用 python3.6.1 遇到同样的错误
-
@araknoid 我正在接受
[*a, *b] -
@cᴏʟᴅsᴘᴇᴇᴅ 哦,我的错。感谢您在答案中指定它。
-
在这种情况下,我认为标题需要进一步澄清。我冒昧地对其进行了编辑。
标签: python python-3.x list list-comprehension nested-lists