首页 > 解决方案 > 按元素加入列表列表

问题描述

我想按元素加入列表列表,这样

lst = [['a','b','c'], [1,2,3], ['x','y','z'], [4,5,6]]

变成

res = [['a',1,'x',4], ['b',2,'y',5], ['c',3,'z',6]]

我尝试过使用列表理解、连接和映射,但还没有任何运气。(蟒蛇新手)

标签: python

解决方案


尝试zip(类似问题:转置列表列表):

>>> lst = [["a","b","c"], [1,2,3], ["x","y","z"], [4,5,6]]
>>> res = [list(zipped) for zipped in zip(*lst)]
>>> res
[['a', 1, 'x', 4], ['b', 2, 'y', 5], ['c', 3, 'z', 6]]

推荐阅读