在python中将元素添加到不相等的列表中

我有两个列表 – :

a=[1,5,3,4,25,6]

b=[10,25,3]

现在我想要这种输出

b =[10,25,3,None,None,None]

为了得到这个输出,我使用了这个

for x,y in itertools.zip_longest(a,b):

但是,这对我没有帮助.我如何获得所需的输出?

after that I want give it size of a list , it doesn’t matter whether we add Zero or None , at the end I want the size of both of those list is the same

任何帮助将不胜感激.

最佳答案 你很亲密你绝对可以使用zip_longest来获得你想要的输出:

from itertools import zip_longest

a = [1, 5, 3, 4, 25, 6]
b = [10, 25, 3]

[y for _, y in zip_longest(a, b)]
# [10, 25, 3, None, None, None]

一个不同的选项,不会不必要地生成压缩对只是为了丢弃每个的一半将使用迭代器和下一个:

it = iter(b)
[next(it, None) for _ in range(len(a))]
# [10, 25, 3, None, None, None]
点赞