如何在Python中压缩两个列表列表?

时间:2011-09-19 17:15:43

标签: python list merge zip nested

我有两个列表,它们具有相同数量的项目。这两个列表如下所示:

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

L2 =[[a, b], [c, d], [e, f]]

我希望创建一个如下所示的列表:

Lmerge = [[1, 2, a, b], [3, 4, c, d], [5, 6, e, f]]

我试图使用zip()这样的东西:

for list1, list2 in zip(*L1, *L2):
    Lmerge = [list1, list2]

组合两个列表列表的最佳方法是什么?提前谢谢。

5 个答案:

答案 0 :(得分:21)

>>> map(list.__add__, L1, L2)
[[1, 2, 'a', 'b'], [3, 4, 'c', 'd'], [5, 6, 'e', 'f']]

答案 1 :(得分:12)

>>> L1 = [[1, 2], [3, 4], [5, 6]]
>>> L2 =[["a", "b"], ["c", "d"], ["e", "f"]]
>>> [x + y for x,y in zip(L1,L2)]
[[1, 2, 'a', 'b'], [3, 4, 'c', 'd'], [5, 6, 'e', 'f']]

或者,

>>> [sum(x,[]) for x in zip(L1,L2)]
[[1, 2, 'a', 'b'], [3, 4, 'c', 'd'], [5, 6, 'e', 'f']]

或,

>>> import itertools
>>> [list(itertools.chain(*x)) for x in zip(L1,L2)]
[[1, 2, 'a', 'b'], [3, 4, 'c', 'd'], [5, 6, 'e', 'f']]

我们也可以在没有zip()的情况下执行此操作:

>>> [L1[i] + L2[i] for i in xrange(min(len(L1), len(L2)))]  
[[1, 2, 'a', 'b'], [3, 4, 'c', 'd'], [5, 6, 'e', 'f']]

>>> [x + L2[i] for i, x in enumerate(L1)]  # assuming len(L1) == len(l2)
[[1, 2, 'a', 'b'], [3, 4, 'c', 'd'], [5, 6, 'e', 'f']]

>>> # same as above, but deals with different lengths
>>> Lx, Ly = ((L2,L1), (L1,L2))[len(L1)<=len(L2)] # shortcut for if/else
>>> [x + Ly[i] for i, x in enumerate(Lx)]

一些基准

以下是迄今为止所提供答案的一些基准。

看起来最流行的答案([x + y for x,y in zip(L1,L2)])几乎与@hammar's map solution相同。 另一方面,我给出的替代解决方案已被证明是垃圾!

然而,最快的解决方案(目前)似乎是在没有zip()的情况下使用列表理解的解决方案。

[me@home]$ SETUP="L1=[[x,x+1] for x in xrange(10000)];L2=[[x+2,x+3] for x in xrange(10000)]"

[me@home]$ # this raises IndexError if len(L1) > len(L2)
[me@home]$ python -m timeit "$SETUP" "[x + L2[i] for i, x in enumerate(L1)]"
100 loops, best of 3: 10.6 msec per loop

[me@home]$ # same as above, but deals with length inconsistencies
[me@home]$ python -m timeit "$SETUP" "Lx,Ly=((L2,L1),(L1,L2))[len(L1)<=len(L2)];[x + Ly[i] for i, x in enumerate(Lx)]"
100 loops, best of 3: 10.6 msec per loop

[me@home]$ # almost as fast as above, but easier to read
[me@home]$ python -m timeit "$SETUP" "[L1[i] + L2[i] for i in xrange(min(len(L1),len(L2)))]"
100 loops, best of 3: 10.8 msec per loop

[me@home]$ python -m timeit "$SETUP" "L3=[x + y for x,y in zip(L1,L2)]"
100 loops, best of 3: 13.4 msec per loop

[me@home]$ python -m timeit "$SETUP" "L3=map(list.__add__, L1, L2)" 
100 loops, best of 3: 13.5 msec per loop

[me@home]$ python -m timeit "$SETUP" "L3=[sum(x,[]) for x in zip(L1,L2)]"
100 loops, best of 3: 18.1 msec per loop

[me@home]$ python -m timeit "$SETUP;import itertools" "L3=[list(itertools.chain(*x)) for x in zip(L1,L2)]"
10 loops, best of 3: 32.9 msec per loop

@Zac's suggestion非常快,但之后我们在这里比较苹果和橙子,因为它在L1上进行了就地列表扩展,而不是创建第三个列表。因此,如果不再需要L1,这是一个很好的解决方案。

[me@home]$ python -m timeit "$SETUP" "for index, x in enumerate(L1): x.extend(L2[index])"
100 loops, best of 3: 9.46 msec per loop

但是,如果L1必须保持不变,那么一旦包含深度复制,性能就会低于标准。

[me@home]$ python -m timeit "$SETUP;from copy import deepcopy" "L3=deepcopy(L1)
> for index, x in enumerate(L1): x.extend(L2[index])"
10 loops, best of 3: 116 msec per loop

答案 2 :(得分:4)

您希望将子列表与plus运算符组合在一起,并在列表解析中迭代它们:

Lmerge = [i1 + i2 for i1, i2 in zip(L1, L2)]

答案 3 :(得分:1)

L1 = [[1, 2], [3, 4], [5, 6]]
L2 =[[a, b], [c, d], [e, f]]

Lmerge = [x + y for x, y in zip(L1, L2)]
[[1, 2, 'a', 'b'], [3, 4, 'c', 'd'], [5, 6, 'e', 'f']]

答案 4 :(得分:1)

for index, x in enumerate(L1):
    x.extend(L2[index])
相关问题