简体   繁体   English

如何根据嵌套列表的索引进行组合?

[英]How to combine nested lists based on their index?

Consider these nested lists: 考虑以下嵌套列表:

L1 = [[1,2,3],[4,5,6],[7,8,9]]
L2 = [[11,22,33],[44,55,66]]
L3 = [[111,222,333],[444,555,666],[777,888,999]]
L4 = [12,13,14] # Note: not a nested list

I require: 我要求:

output1 = [1,4,7,11,44,111,444,777,12]
output2 = [2,5,8,22,55,222,555,888,13]
output3 = [3,6,9,33,66,333,666,999,14]

A oneliner: 一个班轮:

output1, output2, output3 = [[l[i] for l in L1+L2+L3+[L4]] for i in range(3)]

Outputs: 输出:

>>> output1
[1, 4, 7, 11, 44, 111, 444, 777, 12]
>>> output2
[2, 5, 8, 22, 55, 222, 555, 888, 13]
>>> output3
[3, 6, 9, 33, 66, 333, 666, 999, 14]

Though you really should provide a sample of what you tried when asking for a solution :-) 虽然您确实应该提供在寻求解决方案时尝试的示例:-)

Update 更新资料

As we are apparently indulging ourselves with a friendly competition, here's the timing of different answers on my machine (python3): 显然,我们沉迷于一场友好的竞争中,下面是我的机器(python3)上不同答案的时机:

Code: 码:

import time

L1 = [[1,2,3],[4,5,6],[7,8,9]]
L2 = [[11,22,33],[44,55,66]]
L3 = [[111,222,333],[444,555,666],[777,888,999]]
L4 = [12,13,14]


t = time.process_time()

output1, output2, output3 = [[l[i] for l in L1+L2+L3+[L4]] for i in range(3)]

print(output1)
print(output2)
print(output3)

print("%.7f" %(time.process_time() - t))


t = time.process_time()

output1, output2, output3 = map(list,(zip(*L1+L2+L3+[L4])))

print(output1)
print(output2)
print(output3)

print("%.7f" %(time.process_time() - t))


t = time.process_time()

from itertools import chain

output_1, output_2, output_3 = zip(*chain(L1, L2, L3, [L4]))

print(output1)
print(output2)
print(output3)

print("%.7f" %(time.process_time() - t))

Results (of one of the fastest runs): 结果 (最快的运行之一):

[1, 4, 7, 11, 44, 111, 444, 777, 12]
[2, 5, 8, 22, 55, 222, 555, 888, 13]
[3, 6, 9, 33, 66, 333, 666, 999, 14]
0.0000650
[1, 4, 7, 11, 44, 111, 444, 777, 12]
[2, 5, 8, 22, 55, 222, 555, 888, 13]
[3, 6, 9, 33, 66, 333, 666, 999, 14]
0.0000356
[1, 4, 7, 11, 44, 111, 444, 777, 12]
[2, 5, 8, 22, 55, 222, 555, 888, 13]
[3, 6, 9, 33, 66, 333, 666, 999, 14]
0.0002259

@Chris_Rands answer seems to be the fastest :-) @Chris_Rands的回答似乎是最快的:-)

There is a simple solution with zip : zip有一个简单的解决方案:

output1, output2, output3 = map(list,(zip(*L1+L2+L3+[L4])))

Note that if you are content with tuples instead of lists, this can be further simplified: 请注意,如果您对元组而不是列表感到满意,则可以进一步简化:

output1, output2, output3 = zip(*L1+L2+L3+[L4])

I found this to be slightly faster than using chain to merge the lists, at least for this data. 我发现这至少比使用chain合并列表的速度要快,至少对于此数据而言。 It also appears to be faster than @Gormador's solution. 它似乎也比@Gormador的解决方案要快。

>>> def f1():
...     output1, output2, output3 = zip(*L1+L2+L3+[L4])
... 
>>> def f2():
...     output_1, output_2, output_3 = zip(*chain(L1, L2, L3, [L4]))
... 
>>> def f3():
...     output1, output2, output3 = [[l[i] for l in L1+L2+L3+[L4]] for i in range(3)]
... 
>>> timeit(lambda: f1(), number = 1000000)
1.5090796248987317
>>> timeit(lambda: f2(), number = 1000000)
1.7326991918962449
>>> timeit(lambda: f3(), number = 1000000)
5.100052359048277

I am amazed that no one has mentioned the usage of itertools.chain() with zip() . 我很惊讶,没有人提到zip()itertools.chain()的用法。 The simplest solution will be: 最简单的解决方案是:

from itertools import chain

output_1, output_2, output_3 = zip(*chain(L1, L2, L3, [L4]))

which make variables hold the value as: 使变量将值保存为:

>>> output_1
(1, 4, 7, 11, 44, 111, 444, 777, 12)
>>> output_2
(2, 5, 8, 22, 55, 222, 555, 888, 13)
>>> output_3
(3, 6, 9, 33, 66, 333, 666, 999, 14)

Try this: 尝试这个:

L = L1+L2+L3+[L4]
output1 = [l[0] for l in L]
output2 = [l[1] for l in L]
output3 = [l[2] for l in L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM