将3列表映射到python中的单个列表

时间:2017-01-13 04:50:34

标签: python

假设我有3个列表

[1,2,3]
['one','two','three']
['first','second','third']

我需要将它合并到一个列表中,如

[[1,'one','first'],[2,'two','second','third'],[3,'three','third']]

我们如何做到这一点?使用列表理解?还有其他最好的方法吗?

1 个答案:

答案 0 :(得分:3)

使用zip

>>>list(zip([1,2,3],['one','two','three'],['first','second','third']))
[(1, 'one', 'first'), (2, 'two', 'second'), (3, 'three', 'third')]

或列表清单

>>>list(map(list, zip([1,2,3],['one','two','three'],['first','second','third'])))
[[1, 'one', 'first'], [2, 'two', 'second'], [3, 'three', 'third']]

注意:最外面的list调用仅用于提供对map / zip函数的即时评估,如果您稍后将对其进行迭代,则不需要。

相关问题