[英]How to zip two lists of lists in Python?
我有兩個列表,列表具有相同數量的項目。 這兩個列表如下所示:
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]
組合兩個列表列表的最佳方法是什么? 提前致謝。
>>> map(list.__add__, 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"]]
>>> [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
您希望將子列表與plus運算符組合,並在列表解析中迭代它們:
Lmerge = [i1 + i2 for i1, i2 in zip(L1, L2)]
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']]
for index, x in enumerate(L1):
x.extend(L2[index])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.