首页 > 解决方案 > 有没有办法解压缩嵌套冗余列表的列表?

问题描述

我有这个清单:

input = [[[1,2]], [[3,4]], [[5,6]]]

想要的输出:

output = [[1,3,5],[2,4,6]]

我试过这个:

x, y = map(list,zip(*input))

后来意识到这种方法由于多余的方括号而不起作用,有没有一种方法可以在没有迭代的情况下解决这个问题。

标签: python-3.xlist-manipulation

解决方案


In [117]: input = [[[1,2]], [[3,4]], [[5,6]]]

In [118]: list(zip(*[i[0] for i in input]))
Out[118]: [(1, 3, 5), (2, 4, 6)]

In [119]: list(map(list, zip(*[i[0] for i in input])))
Out[119]: [[1, 3, 5], [2, 4, 6]]

推荐阅读