Python itertools教程(python中的迭代器与组合迭代器)

当谈到在Python中优雅地处理迭代任务时,itertools库是一个不可或缺的工具。这个强大的库提供了一系列用于创建、操作和合并迭代器的函数,能够让你的代码更加紧凑、高效,并且更易于阅读和维护。无论是解决排列组合问题、处理无限序列,还是简化循环结构,itertools都能够成为你的得力助手。

迭代器:无处不在的序列掌舵者

在深入探讨itertools之前,让我们先来了解一下什么是迭代器。在Python中,迭代器是一种对象,它可以逐个地返回元素,而无需事先将所有元素加载到内存中。这在处理大型数据集时尤为重要,因为它允许我们按需处理数据,而不必一次性加载全部内容。

例如,你可以使用以下方式创建一个简单的迭代器,以输出从1到5的数字:

class SimpleIterator:
    def __init__(self, start, end):
        self.current = start
        self.end = end

    def __iter__(self):
        return self

    def __next__(self):
        if self.current <= self.end:
            value = self.current
            self.current += 1
            return value
        else:
            raise StopIteration

# 使用自定义迭代器输出数字
my_iterator = SimpleIterator(1, 5)
for num in my_iterator:
    print(num)

然而,Python的itertools库将迭代器的创建和操作提升到了一个全新的水平,让我们能够以更简洁的方式处理各种迭代任务。

1.count(start=0, step=1)

count() 函数可以接受两个参数:start 和 step。它会从 start 开始,以步长为 step 生成一个值序列。

from itertools import count

# 从10开始,步长为2的计数器
for i in count(10, 2):
    print(i)
    if i > 25:
        break

输出:

10
12
14
16
18
20
22
24
26

此外,count() 也可以只使用一个参数,表示从该值开始,步长为1:

from itertools import count

# 从2开始,步长为1的计数器
for i in count(2):
    print(i)

输出:

2
3
4
5
6
7
...

如果不传入任何参数,默认从0开始:

from itertools import count

# 从0开始,步长为1的计数器
for i in count():
    print(i)

输出:

0
1
2
3
4
5
...

请注意,当使用无限循环时,最好加上终止条件以避免无限循环。在上述示例中,我在 for 循环中添加了 if i > 25: break 以确保循环在某个条件下终止。

通过使用 count() 函数,你可以轻松生成各种连续数字序列,并根据需要调整起始值和步长。这在许多数字生成的场景中都非常有用,如生成时间序列、索引等。

2.cycle(iterable)

cycle() 函数会从可迭代对象中生成一个迭代器,并保存其元素的副本。一旦原始可迭代对象的元素被耗尽,它会无限循环地返回这些副本中的元素。

from itertools import cycle

colors = cycle(['red', 'green', 'blue'])
for color in colors:
    print(color)

输出:

red
green
blue
red
green
blue
red
green
blue
...

cycle() 函数可以处理任何类型的可迭代对象

3.repeat(elem [,n])

repeat() 函数会将元素 elem 重复生成 n 次,或者如果不提供 n,则会无限次重复生成到迭代器中。

from itertools import repeat

# 将元素 'Red' 重复生成 3 次
for i in repeat('Red', 3):
    print(i)

输出:

Red
Red
Red

如果不指定重复次数,它会无限次重复:

from itertools import repeat

# 将元素 'Red' 无限次重复生成
for i in repeat('Red'):
    print(i)

输出:

Red
Red
Red
Red
Red
...

在使用 repeat() 时,要注意避免无限循环。与之前提到的函数一样,在处理无限循环时最好添加终止条件,以确保循环不会无限运行。

通过使用 cycle() 和 repeat() 函数,你可以轻松地生成重复序列,无论是从一个固定的集合中循环获取元素,还是生成一组重复的值。这在创建模拟数据或执行特定重复操作时非常有用。

Python中的组合迭代器

组合(Combinatorial)指的是离散数学元素的排列、操作和选择。现在,让我们来谈谈关于组合迭代器的内容。

1. product(*iterables, repeat=1)

product() 函数返回输入可迭代对象的笛卡尔积。这等效于一个嵌套的 for 循环。

from itertools import product

# 两个可迭代对象的笛卡尔积
for i in product([1, 2, 3], [4, 5, 6]):
    print(i)

输出:

(1, 4)
(1, 5)
(1, 6)
(2, 4)
(2, 5)
(2, 6)
(3, 4)
(3, 5)
(3, 6)

这与使用生成器对象进行的嵌套 for 循环等效:

for i in ((i, j) for i in [1, 2, 3] for j in [4, 5, 6]):
    print(i)

输出同上。

product() 函数可以处理任何类型的可迭代对象,并生成它们的笛卡尔积。

2. permutations(iterable, r=None)

permutations() 函数返回可迭代对象中长度为 r 的排列。

它按字典顺序生成所有可能的排列,且元素不重复。

from itertools import permutations

# 'ABCD' 中的排列
for i in permutations('ABCD'):
    print(i)

输出:

('A', 'B', 'C', 'D')
('A', 'B', 'D', 'C')
('A', 'C', 'B', 'D')
('A', 'C', 'D', 'B')
...

如果不传入第二个参数,它会生成与可迭代对象长度相等的元组。

3. combinations(iterable, r)

combinations() 函数从可迭代对象中生成长度为 r 的子序列。

from itertools import combinations

# 'ABCD' 中的长度为 2 的子序列
for i in combinations('ABCD', 2):
    print(i)

输出:

('A', 'B')
('A', 'C')
('A', 'D')
('B', 'C')
('B', 'D')
('C', 'D')

combinations() 生成的元组是按照字典顺序递增的。

4. combinations_with_replacement(iterable, r)

combinations_with_replacement() 函数返回长度为 r 的子序列,其中元素可能重复。

from itertools import combinations_with_replacement as cwr

# 'ABCD' 中的长度为 2 的子序列,元素可能重复
for i in cwr('ABCD', 2):
    print(i)

输出:

('A', 'A')
('A', 'B')
('A', 'C')
('A', 'D')
('B', 'B')
('B', 'C')
('B', 'D')
('C', 'C')
('C', 'D')
('D', 'D')

通过使用这些组合迭代器函数,你可以在处理排列组合问题时,以一种优雅、高效的方式生成所需的组合序列。无论是生成元素排列、子序列还是笛卡尔积,itertools 都能让你的代码变得更加简洁和易于理解。

在最短输入序列上终止的迭代器

1. accumulate(iterable [,func])

accumulate() 函数创建一个累积求和的迭代器(或根据指定的二元函数进行累积运算)。

from itertools import accumulate

# 对列表中的元素进行累积求和
for i in accumulate([0, 1, 0, 1, 1, 2, 3, 5]):
    print(i)

输出:

0
1
1
2
3
4
7
12

这也可以用来打印斐波那契数列。

另一个例子:

import operator

# 使用乘法函数对列表中的元素进行累积运算
for i in accumulate([1, 2, 3, 4, 5], operator.mul):
    print(i)

输出:

1
2
6
24
120

2. chain(*iterables)

chain() 函数从第一个可迭代对象开始生成迭代器,然后是第二个,以此类推。

一旦一个可迭代对象耗尽,它会切换到下一个可迭代对象。

from itertools import chain

# 连接多个可迭代对象的元素
for i in chain('Hello', 'World', 'Bye'):
    print(i)

输出:

H
e
l
l
o
W
o
r
l
d
B
y
e

3. chain.from_iterable(iterable)

chain.from_iterable() 是 chain() 的替代构造函数。它从单个可迭代对象参数中获取链接的输入,并以惰性方式进行评估。

from itertools import chain

# 与上例相同,但不需要额外导入
for i in chain.from_iterable(['Hello', 'World', 'Bye']):
    print(i)

输出同上。

4. compress(data, selectors)

compress() 函数根据选择器值为 True 的情况,对数据进行过滤。

from itertools import compress

# 根据选择器过滤数据
for i in compress('ABCDEF', [1, 0, 1, True, 0, ' ']):
    print(i)

输出:

A
C
D
F

5. dropwhile(predicate, iterable)

只要谓词为 True,就从可迭代对象中丢弃元素。一旦谓词为 False,就开始返回每个元素。

from itertools import dropwhile

# 丢弃满足条件的元素
for i in dropwhile(lambda x: x < 7, [1, 2, 7, 9, 5, 3, 2, 9]):
    print(i)

输出:

7
9
5
3
2
9

6. filterfalse(predicate, iterable)

filterfalse() 函数根据谓词为 True 的情况,对可迭代对象中的元素进行过滤。

from itertools import filterfalse

# 根据条件过滤元素
for i in filterfalse(lambda x: x < 7, [1, 2, 7, 9, 5, 3, 2, 9]):
    print(i)

输出:

7
9
9
# 根据条件过滤奇数
for i in filterfalse(lambda x: x % 2, [1, 2, 7, 9, 5, 3, 2, 9]):
    print(i)

输出:

2
2
7

在使用 filterfalse() 时,根据谓词的真值来过滤可迭代对象中的元素。

7. groupby(iterable, key=None)

groupby() 函数创建一个迭代器,它将可迭代对象的元素按照键分组。

from itertools import groupby

# 按照键进行分组
for key, group in groupby('AAAAABBCCCCCDDDCCCBBA'):
    print(list(group))

输出:

['A', 'A', 'A', 'A', 'A']
['B', 'B']
['C', 'C', 'C', 'C', 'C']
['D', 'D', 'D']
['C', 'C', 'C']
['B', 'B']
['A']

groupby() 根据键将连续的元素分组成子迭代器。

8. islice(iterable, stop)

islice() 函数创建一个迭代器,从可迭代对象中选择指定的元素。

from itertools import islice

# 选择前 2 个元素
for i in islice([1, 2, 3, 4, 5], 2):
    print(i)

输出:

1
2
# 选择索引从 2 到 4 的元素
for i in islice([1, 2, 3, 4, 5], 2, 5):
    print(i)

输出:

3
4
5
# 选择索引从 0 到 4,步长为 2 的元素
for i in islice([1, 2, 3, 4, 5], 0, 5, 2):
    print(i)

输出:

1
3
5

9. starmap(function, iterable)

starmap() 函数根据可迭代对象中的参数计算结果。

from itertools import starmap

# 对参数进行减法计算
for i in starmap(operator.sub, [(2, 1), (7, 3), (15, 10)]):
    print(i)

输出:

1
4
5

10. takewhile(predicate, iterable)

takehile() 函数创建一个迭代器,它从可迭代对象中返回元素,只要谓词为 True。与 dropwhile() 正好相反。

from itertools import takewhile

# 返回满足条件的元素
for i in takewhile(lambda x: x < 7, [1, 2, 7, 9, 5, 3, 2, 9]):
    print(i)

输出:

1
2

11. tee(n=2)

tee() 函数将一个迭代器拆分为 n 个独立的迭代器。

from itertools import tee

# 将一个迭代器拆分为 3 个独立的迭代器
for i in tee([1, 2, 3, 4, 5, 6, 7], 3):
    for j in i:
        print(j)
    print()

输出:

1
2
3
4
5
6
7

1
2
3
4
5
6
7

1
2
3
4
5
6
7

12. zip_longest(*iterables, fillvalue=None)

zip_longest() 函数通过聚合每个可迭代对象的元素创建一个迭代器。fillvalue 参数指定了较短可迭代对象中剩余位置的填充值。

from itertools import zip_longest

# 聚合两个可迭代对象的元素,填充为 '*'
for i in zip_longest('ABC', '12345', fillvalue='*'):
    print(i)

输出:

('A', '1')
('B', '2')
('C', '3')
('*', '4')
('*', '5')
# 聚合三个可迭代对象的元素,填充为 '*'
for i in zip_longest('ABC', '12345', 'Hello', fillvalue='*'):
    print(i)

输出:

('A', '1', 'H')
('B', '2', 'e')
('C', '3', 'l')
('*', '4', 'l')
('*', '5', 'o')

以上这些迭代器函数可以在不同场景中帮助你处理迭代数据,进行累积、分组、选择等各种操作。通过使用它们,你可以更高效地处理和操作数据序列。

猜你喜欢

转载自blog.csdn.net/weixin_42010722/article/details/132540679
今日推荐