这个答案https://stackoverflow.com/a/7947190/5517459给出:
>>> l1=[1,2,3]
>>> l2=[10,20,30]
>>> l3=[101,102,103,104]
>>> [y for x in map(None,l1,l2,l3) for y in x if y is not None]
[1, 10, 101, 2, 20, 102, 3, 30, 103, 104]
这个片段完全符合我的项目要求,但它在 python3 中不起作用。
我已经使用以下方法解决了错误:
func = lambda *x: x
modules = [y for x in map(func,l1,l2,l3) for y in x]
但它现在不能处理可变长度的列表,一旦它用完最短的列表就会停止。
看起来你需要 itertools.zip_longest
from itertools import zip_longest
l1=[1,2,3]
l2=[10,20,30]
l3=[101,102,103,104]
print([y for x in zip_longest(l1,l2,l3, fillvalue=None) for y in x if y is not None])
输出:
[1, 10, 101, 2, 20, 102, 3, 30, 103, 104]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句