我有这个清单:
input = [[[1,2]], [[3,4]], [[5,6]]]
想要的输出:
output = [[1,3,5],[2,4,6]]
我试过这个:
x, y = map(list,zip(*input))
后来意识到这种方法由于多余的方括号而不起作用,有没有一种方法可以在没有迭代的情况下解决这个问题。
我有这个清单:
input = [[[1,2]], [[3,4]], [[5,6]]]
想要的输出:
output = [[1,3,5],[2,4,6]]
我试过这个:
x, y = map(list,zip(*input))
后来意识到这种方法由于多余的方括号而不起作用,有没有一种方法可以在没有迭代的情况下解决这个问题。
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]]
你可以试试这个:
>>> from operator import itemgetter
>>> input = [[[1,2]], [[3,4]], [[5,6]]]
>>> list(zip(*map(itemgetter(0), input)))
[(1, 3, 5), (2, 4, 6)]