加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 11:08:48 所属栏目:Python 来源:网络整理
导读:我有两个列表 – : a=[1,5,3,4,25,6] 和 b=[10,3] 现在我想要这种输出 b =[10,None,None] 为了得到这个输出,我使用了这个 for x,y in itertools.zip_longest(a,b): 但是,这对我没有帮助.我如何获得所需的输出? after that I want give it size of a list,i
我有两个列表 – :

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

b=[10,3]

现在我想要这种输出

b =[10,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,6]
b = [10,3]

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读