首页后端开发Pythonpython中itertools模块怎样使用?一文带你看懂

python中itertools模块怎样使用?一文带你看懂

时间2024-03-23 20:58:03发布访客分类Python浏览1251
导读:这篇文章给大家分享的是python中itertools模块的使用。小编觉得挺实用的,因此分享给大家做个参考,文中示例代码介绍的非常详细,感兴趣的朋友接下来一起跟随小编看看吧。Python 内置的 itertools 模块包含了一系列用来产生...

这篇文章给大家分享的是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模块的使用有帮助,想要了解更多可以继续浏览网络其他相关的文章。

文本转载自脚本之家

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: python中itertools模块怎样使用?一文带你看懂
本文地址: https://pptw.com/jishu/651594.html
PHP中for循环语句和foreach语句怎么遍历数组 MySQL中权限类型有哪些,如何设置

游客 回复需填写必要信息