【问题标题】:How to zip nested lists in python如何在python中压缩嵌套列表
【发布时间】:2025-12-06 18:45:02
【问题描述】:

如何压缩任意数量子列表的列表,以便只接收所有合并的子元素?

有:

[[1,...],[2,...],[3,...],...]

想要:

[1, 2, 3,...]

【问题讨论】:

  • 你已经有了答案here
  • [[1,2,3], [4, 5], [6]] 的输出应该是什么?
  • @JacksonPro 不,这不是完全相同的答案,只是其中的一部分。结果应该是[1, 4, 6]
  • 好的,谢谢澄清。

标签: python list-comprehension nested-lists


【解决方案1】:

使用嵌套列表理解和星号参数

list_of_lists = [[1],[2],[3]]
[j for i in zip(*list_of_lists) for j in i]

>>> [1, 2, 3]

【讨论】: