Python Itertools2025 年 8 月 28 日 | 阅读 9 分钟 Itertools 是 Python 3 标准库中最令人惊叹的库之一。这个库拥有许多很酷的函数,毫不夸张地说,它是 Python 编程语言的瑰宝。Python 提供了关于 itertools 的精彩文档,但在本教程中,我们将讨论几个重要且有用的 itertools 函数或迭代器。 itertools 的关键在于,这个库的函数用于编写内存效率高且精确的代码。 在学习 Python itertools 之前,你应该了解 Python 迭代器和生成器。在本文中,我们将为初学者和专业人士介绍 itertools。 引言根据 itertools 的官方定义,“该模块实现了许多受 APL、Haskell 和 SML 构造启发的迭代器构建块”。简单来说,许多迭代器可以一起创建“迭代器代数”,从而能够完成复杂的任务。itertools 中的函数用于生成更复杂的迭代器。我们来看一个例子:Python 内置的 zip() 函数接受任意数量的可迭代对象作为参数。它迭代元组并返回它们对应的元素。 输出 [(1, 'a'), (2, 'b'), (3, 'c')] 在上面的代码中,我们将两个列表 [1,2,3] 和 ['a', 'b', 'c'] 作为可迭代对象传递给 zip() 函数。这些列表一次返回一个元素。在 Python 中,实现 .__iter__() 或 .__getitem__() 方法的对象称为可迭代对象。 Python 的 iter() 函数用于在可迭代对象上调用并返回该可迭代对象的迭代器对象。 输出 <str_iterator object at 0x01505FA0> Python 的 zip() 函数会调用其每个参数上的 iter(),然后通过将结果组合成元组来调用 next()。 注意:如果你正在使用 zip() 函数和 map() 函数,这意味着你已经在使用了 itertools。你不需要单独导入它。迭代器类型itertools 模块中有多种类型的迭代器。列表如下
无限迭代器在 Python 中,任何实现 for 循环的对象都称为迭代器。列表、元组、集合、字典、字符串都是迭代器的例子,但迭代器也可以是无限的,这种迭代器称为无限迭代器。
输出 10 15 20 25 30 35 40 45
输出 1 2 3 1 2 3 1 2 3 1 2 示例 - 2:使用 next() 函数 输出 Java T Point Java T Point
输出 [40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40] 组合迭代器:复杂的组合结构可以通过递归生成器进行简化。排列、组合和笛卡尔积是组合构造的例子。 在 Python 中,有四种组合迭代器
输出 Computing cartesian product using repeat Keyword Argument: [(1, 1), (1, 2), (2, 1), (2, 2)] Computing cartesian product of the containers: [('Java', '5'), ('T', '5'), ('point', '5')] Computing product of the containers: [('C', 4), ('C', 5), ('D', 4), ('D', 5)]
输出 Computing all permutation of the following list [(3, 'Python'), ('Python', 3)] Permutations of following string [('A', 'B'), ('B', 'A')] Permutation of the given container is: [(0, 1), (0, 2), (0, 3), (1, 0), (1, 2), (1, 3), (2, 0), (2, 1), (2, 3), (3, 0), (3, 1), (3, 2)]
输出 Combination of list in sorted order(without replacement) [('B', 3)] Combination of string in sorted order [('Z', 'X')] Combination of list in sorted order [(0,), (1,), (2,), (3,), (4,), (5,), (6,), (7,), (8,), (9,)]
输出 Combination of string in sorted order(with replacement) is: [('X', 'X', 'X'), ('X', 'X', 'Y'), ('X', 'Y', 'Y'), ('Y', 'Y', 'Y')] Combination of list in sorted order(with replacement) is: [(4, 4, 4), (4, 4, 2), (4, 2, 2), (2, 2, 2)] Combination of container in sorted order(with replacement) is: [(0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 2)] 终止迭代器终止迭代器通常用于处理小型输入序列,并根据所用迭代器功能的函数生成输出。 有不同类型的终止迭代器
输出 The sum is : [1, 5, 10, 17, 26, 37] The product is : [1, 4, 20, 140, 1260, 13860] The sum is : [1, 5, 10, 17, 26, 37] The product is : [1, 4, 20, 140, 1260, 13860]
输出 The output is: [1, 2, 3, 4, 1, 5, 6, 8, 9, 10, 11, 12]
输出 The output is : [5, 7, 8]
输出 The Output is : [15, 27]
输出 The sliced list values are : [34, 73, 19]
输出 The values acc. to function are : [20, 40, 90, 27]
输出 The list values until false value return : [20, 42, 64]
输出 (<itertools._tee object at 0x01B88D88>, <itertools._tee object at 0x01B88DA8>, <itertools._tee object at 0x01B88BA8>) The iterators are : [1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5, 6, 7]
输出 The combined value of iterables is : ('J', 'T') ('a', 'p') ('v', 'o') ('a', 'i') ('_', 'n') ('_', 't') 在本教程中,我们讨论了几种有用的迭代器以及 itertools。 |
我们请求您订阅我们的新闻通讯以获取最新更新。