如何压缩两个列表列表

发布于 2024-12-05 09:11:15 字数 383 浏览 0 评论 0原文

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

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]

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

I have two lists of lists that have equivalent numbers of items. The two lists look like this:

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

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

I am looking to create one list that looks like this:

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

I was attempting to use zip() something like this:

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

What is the best way to combine two lists of lists? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

轮廓§ 2024-12-12 09:11:15
>>> map(list.__add__, L1, L2)
[[1, 2, 'a', 'b'], [3, 4, 'c', 'd'], [5, 6, 'e', 'f']]
>>> map(list.__add__, L1, L2)
[[1, 2, 'a', 'b'], [3, 4, 'c', 'd'], [5, 6, 'e', 'f']]
水溶 2024-12-12 09:11:15
>>> 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 的 map 解决方案另一方面,我给出的替代解决方案已被证明是垃圾!

但是,最快的解决方案(目前)似乎是使用列表理解而不使用 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 的建议非常快,但是我们在这里比较苹果和橙子,因为它在 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
>>> 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']]

Or,

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

or,

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

We can also do it without 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)]

Some benchmarks

Here are some benchmarks for the answers provided so far.

It looks like the most popular answer ([x + y for x,y in zip(L1,L2)]) is pretty much on par with @hammar's map solution. On the other hand, the alternative solutions I've given have proven to be rubbish!

However, the fastest solutions (for now) seems to be the ones that uses list comprehension without 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 is really quick, but then we're comparing apples and oranges here since it does a list extension in-place on L1 instead of creating a third list. So, if L1 is not longer needed, this is a great solution.

[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

However, if L1 has to be kept intact, then performance would be sub par once you include the deepcopy.

[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
梦归所梦 2024-12-12 09:11:15

您想要将子列表与加号运算符组合起来,并在列表理解中迭代它们:

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

You want to combine the sublists with the plus operator, and iterate over them in a list comprehension:

Lmerge = [i1 + i2 for i1, i2 in zip(L1, L2)]
放飞的风筝 2024-12-12 09:11:15
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']]
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']]
月朦胧 2024-12-12 09:11:15
for index, x in enumerate(L1):
    x.extend(L2[index])
for index, x in enumerate(L1):
    x.extend(L2[index])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文