ITPub博客

首页 > IT职业 > IT生活 > python zip_longest如何使用

python zip_longest如何使用

IT生活 作者:mahaijiang 时间:2021-09-11 09:53:18 0 删除 编辑

使用说明

1、zip_longest需要导入itertools模块,且使用的时候需要指定一个填充值fillvalue。

2、当有可迭代对象遍历完,但其他对象还没有的时候,缺少的相应元素就会使用填充值进行填充。

实例

from itertools import zip_longest
a = [i for i in range(10)]
b = [i for i in range(1, 9)]
for num1, num2 in zip_longest(a, b, fillvalue=-1):
    print(num1, num2)
# 0 1
# 1 2
# 2 3
# 3 4
# 4 5
# 5 6
# 6 7
# 7 8
# 8 -1
# 9 -1

以上就是python zip_longest的使用,希望对大家有所帮助。更多Python学习指路:python基础教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3071/viewspace-2829618/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 爬虫怎么运行
请登录后发表评论 登录
全部评论

注册时间:2019-08-13

  • 博文量
    76
  • 访问量
    779