您现在的位置是:群英 > 开发技术 > Python语言
python中itertools模块怎样使用?一文带你看懂
Admin发表于 2021-11-22 17:53:561231 次浏览

    这篇文章给大家分享的是python中itertools模块的使用。小编觉得挺实用的,因此分享给大家做个参考,文中示例代码介绍的非常详细,感兴趣的朋友接下来一起跟随小编看看吧。

    Python 内置的 itertools 模块包含了一系列用来产生不同类型迭代器的函数或类,这些函数的返回都是一个迭代器,我们可以通过 for 循环来遍历取值,也可以使用 next() 来取值。

    itertools 模块提供的迭代器函数有以下几种类型:

  • 无限迭代器:生成一个无限序列,比如自然数序列 1, 2, 3, 4, ...;
  • 有限迭代器:接收一个或多个序列(sequence)作为参数,进行组合、分组和过滤等;
  • 组合生成器:序列的排列、组合,求序列的笛卡儿积等。

itertools

    高效循环下创建循环器的标准库

Infinite itertools,无限迭代器

    itertools.count(start=0, step=10)

    默认返回一个从0开始,依次+10的自然数迭代器,如果你不停止,它会一直运行下去。可以用start指定开始的位置,step是步长。

import itertools

c = itertools.count(start=0, step=1)
for i in c:
    print(i)

    
# 0
# 10
# 20
# 30
# 40
# 50
# ...

    itertools.cycle(iterable)

    传入一个可迭代对象,然后无限循环迭代。

import itertools

# itertools.count()
l = [1,2,3,4,5]
c = itertools.cycle(l)

for i in c:
    print(i)


# 1
# 2
# 3
# 4
# 5
# 1
# 2
# 3
# 4
# 5
# ...

    itertools.repeat(p_object, times=None)

    重复迭代一个对象p_object,如果不指定times,则会迭代无数次,也可以通过times参数指定迭代的次数。

import itertools

# itertools.count()
l = [1,2,3,4,5]
c = itertools.repeat(l, 5)

for i in c:
    print(i)


# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]

Iterators terminating on the shortest input sequence

    itertools.accumulate(iterable, func)

    返回序列的累计值或者其他二进制函数。

import itertools

# itertools.count()
l = [1,2,3,4,5]
c = itertools.accumulate(l)

print(c)

for i in c:
    print(i)


# 1
# 3
# 6
# 10
# 15

    accumulate()仍然返回的是一个迭代器,传一个list,在for循环中遍历打印的时候发现,它做了累加操作。(迭代第一个数,就是前一个数的和,迭代到第二个数时,就是前两个数的和,以此类推)

    并且做递加操作时支持:list, tuple, str, set, dict

    传入的是dict对象,那么会累加迭代dict的key:

import itertools

# itertools.count()
d = {'a': 1, 'b': 2, 'c': 3}
c = itertools.accumulate(d)

print(c)

for i in c:
    print(i)


# <itertools.accumulate object at 0x000001F7A0A90E48>
# a
# ab
# abc

    itertools.chain(*iterables)

    chain()方法中的参数可以传入多个序列,而且只要是序列即可,不限定序列的数据类型。

    如:迭代list, tuple, str三个序列

import itertools

l = [1, 2, 3, 4, 5]
t = (1, 2, 3, 4, 5)
s = 'abcdefg'
c = itertools.chain(l, t, s)

print(c)

for i in c:
    print(i)

# <itertools.chain object at 0x0000026E64801448>
# 1
# 2
# 3
# 4
# 5
# 1
# 2
# 3
# 4
# 5
# a
# b
# c
# d
# e
# f
# g

itertools 笛卡尔积

import itertools

d = [
    [{"a": 1}, {"b": 2}], [{"c": 3}, {"d": 4}, {"e": 5}], [{"f": 6}, {"g": 7}]
]

print(*d)

for i in itertools.product(*d):
    print(i)

# [{'a': 1}, {'b': 2}] [{'c': 3}, {'d': 4}, {'e': 5}] [{'f': 6}, {'g': 7}]
# ({'a': 1}, {'c': 3}, {'f': 6})
# ({'a': 1}, {'c': 3}, {'g': 7})
# ({'a': 1}, {'d': 4}, {'f': 6})
# ({'a': 1}, {'d': 4}, {'g': 7})
# ({'a': 1}, {'e': 5}, {'f': 6})
# ({'a': 1}, {'e': 5}, {'g': 7})
# ({'b': 2}, {'c': 3}, {'f': 6})
# ({'b': 2}, {'c': 3}, {'g': 7})
# ({'b': 2}, {'d': 4}, {'f': 6})
# ({'b': 2}, {'d': 4}, {'g': 7})
# ({'b': 2}, {'e': 5}, {'f': 6})
# ({'b': 2}, {'e': 5}, {'g': 7})

    以上就是关于python中itertools模块的使用介绍,上述示例具有一定的借鉴价值,有需要的朋友可以参考学习,希望对大家学习itertools模块的使用有帮助,想要了解更多可以继续浏览群英网络其他相关的文章。

文本转载自脚本之家

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

相关信息推荐
2022-06-02 17:21:33 
摘要:方法:1、用array_intersect()比较两个数组的值,语法“array_intersect(数组1,数组2)”,会返回一个交集数组;2、判断交集数组是否为空,语法“交集数组==[]”,如果为空则没有相同值,如果不为空则有相同值。
2022-11-11 17:47:01 
摘要:虚函数和纯虚函数的区别:1、纯虚函数只有定义,没有实现;而虚函数既有定义,也有实现的代码。2、包含纯虚函数的类不能定义其对象,而包含虚函数的则可以。
2022-10-12 17:46:40 
摘要:后端业务开发,每个表都要用到单表的 增删改查 等通用方法,而配置了通用Mapper可以极大的方便使用Mybatis单表的增删改查操作,这篇文章主要介绍了SpringBoot快速整合通用Mapper,需要的朋友可以参考下
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部