小编典典

将3个列表合并为1个列表

python

我想将3个列表合并到一个列表中。例如,我有三个列表:

a = [0, 3, 6, 9]
b = [1, 4, 7, 10]
c = [2, 5, 8, 11]

最后我想得到

merged = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

abc

有没有更快的方法来合并这三个列表?这是我的代码:

merged = []
a = [0, 3, 6, 9]
b = [1, 4, 7, 10]
c = [2, 5, 8, 11]
for i in range(0, len(a)):
    merged.append(a[i])
    merged.append(b[i])
    merged.append(c[i])

阅读 318

收藏
2021-01-20

共1个答案

小编典典

import itertools as it

list(it.chain.from_iterable(it.izip(a,b,c)))
2021-01-20