“Python/docs/3.9/howto/functional”的版本间差异
(autoload) |
小 (Page commit) |
||
第1行: | 第1行: | ||
+ | {{DISPLAYTITLE:函数式编程 HOWTO — Python 文档}} | ||
<div id="functional-programming-howto" class="section"> | <div id="functional-programming-howto" class="section"> | ||
− | = | + | = 函数式编程HOWTO = |
− | + | <dl> | |
− | + | <dt>作者</dt> | |
− | + | <dd><ol> | |
− | + | <li><ol start="13"> | |
+ | <li><p>库克林</p></li></ol> | ||
+ | </li></ol> | ||
+ | </dd> | ||
+ | <dt>发布</dt> | ||
+ | <dd><p>0.32</p></dd></dl> | ||
− | + | 在本文档中,我们将了解适合以函数式风格实现程序的 Python 特性。 在介绍了函数式编程的概念之后,我们将看看语言特性,例如 [[../../glossary#term-iterator|iterator]]s 和 [[../../glossary#term-generator|generator]]s 以及相关的库模块,例如 [[../../library/itertools#module-itertools|itertools]] 和[[../../library/functools#module-functools|functools]]。 | |
− | |||
− | |||
− | [[../../glossary#term-iterator| | ||
− | [[../../library/itertools#module-itertools| | ||
<div id="introduction" class="section"> | <div id="introduction" class="section"> | ||
− | == | + | == 介绍 == |
− | + | 本节解释函数式编程的基本概念; 如果您只是对了解 Python 语言功能感兴趣,请跳到下一部分 [[#functional-howto-iterators|Iterators]]。 | |
− | |||
− | |||
− | + | 编程语言支持以多种不同方式分解问题: | |
− | * | + | * 大多数编程语言都是 '''过程化''' :程序是指令列表,告诉计算机如何处理程序的输入。 C、Pascal 甚至 Unix shell 都是过程语言。 |
− | * | + | * 在 '''声明性''' 语言中,您编写描述要解决的问题的规范,语言实现会弄清楚如何有效地执行计算。 SQL 是您最有可能熟悉的声明性语言; SQL 查询描述了您要检索的数据集,SQL 引擎决定是扫描表还是使用索引,应先执行哪些子条款等。 |
− | * ''' | + | * '''面向对象的'''程序操作对象的集合。 对象具有内部状态并支持以某种方式查询或修改此内部状态的方法。 Smalltalk 和 Java 是面向对象的语言。 C++ 和 Python 是支持面向对象编程的语言,但不强制使用面向对象的特性。 |
− | * ''' | + | * '''函数式''' 编程将问题分解为一组函数。 理想情况下,函数只接受输入并产生输出,并且没有任何影响给定输入产生的输出的内部状态。 著名的函数式语言包括 ML 系列(标准 ML、OCaml 和其他变体)和 Haskell。 |
− | + | 某些计算机语言的设计者选择强调一种特定的编程方法。 这通常会使编写使用不同方法的程序变得困难。 其他语言是支持多种不同方法的多范式语言。 Lisp、C++ 和 Python 是多范式的; 您可以使用所有这些语言编写主要是过程性、面向对象或功能性的程序或库。 在一个大型程序中,可能会使用不同的方法编写不同的部分; 例如,GUI 可能是面向对象的,而处理逻辑是程序性的或功能性的。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | 在函数式程序中,输入流经一组函数。 每个函数对其输入进行操作并产生一些输出。 函数式风格不鼓励具有副作用的函数,这些函数会修改内部状态或进行在函数返回值中不可见的其他更改。 完全没有副作用的函数称为'''纯函数'''。 避免副作用意味着不使用随着程序运行而更新的数据结构; 每个函数的输出必须只依赖于它的输入。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | 有些语言对纯度非常严格,甚至没有赋值语句,例如 <code>a=3</code> 或 <code>c = a + b</code>,但很难避免所有副作用,例如打印到屏幕或写入磁盘文件。 另一个例子是调用 [[../../library/functions#print|print()]] 或 [[../../library/time#time|time.sleep()]] 函数,这两个函数都没有返回有用的值。 两者都只是因为它们向屏幕发送一些文本或暂停执行一秒钟的副作用而被调用。 | |
− | |||
− | |||
− | |||
− | [[../../library/time#time| | ||
− | |||
− | |||
− | Python | + | 以函数式风格编写的 Python 程序通常不会走到避免所有 I/O 或所有分配的极端; 相反,他们将提供一个功能性的界面,但会在内部使用非功能性的特性。 例如,函数的实现仍然会使用对局部变量的赋值,但不会修改全局变量或产生其他副作用。 |
− | |||
− | |||
− | |||
− | |||
− | + | 函数式编程可以被认为是面向对象编程的对立面。 对象是包含一些内部状态以及允许您修改此状态的一组方法调用的小胶囊,程序由进行正确的状态更改集组成。 函数式编程希望尽可能避免状态更改,并处理函数之间的数据流动。 在 Python 中,您可以通过编写接受和返回表示应用程序中的对象(电子邮件消息、事务等)的实例的函数来组合这两种方法。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | 功能设计似乎是一个奇怪的工作限制。 为什么要避免对象和副作用? 函数式风格具有理论和实践优势: | |
− | |||
− | |||
− | * | + | * 正式的可证明性。 |
− | * | + | * 模块化。 |
− | * | + | * 可组合性。 |
− | * | + | * 易于调试和测试。 |
<div id="formal-provability" class="section"> | <div id="formal-provability" class="section"> | ||
− | === | + | === 形式证明 === |
− | + | 理论上的好处是更容易构建函数式程序正确的数学证明。 | |
− | |||
− | + | 长期以来,研究人员一直对寻找数学证明程序正确的方法感兴趣。 这不同于在大量输入上测试程序并得出其输出通常是正确的结论,或阅读程序的源代码并得出代码看起来正确的结论; 相反,目标是严格证明程序为所有可能的输入产生正确的结果。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | 用于证明程序正确的技术是写下 '''不变量''' 、输入数据和程序变量的属性,这些属性始终为真。 对于每一行代码,你然后证明如果不变量 X 和 Y 为真 '''before''' 行被执行,略有不同的不变量 X' 和 Y' 为真 '''after''' 行是执行。 这一直持续到程序结束,此时不变量应该与程序输出的所需条件相匹配。 | |
− | |||
− | |||
− | '''before''' | ||
− | |||
− | |||
− | |||
− | + | 函数式编程避免赋值的出现是因为这种技术很难处理赋值; 赋值可以破坏赋值之前为真的不变量,而不会产生任何可以向前传播的新不变量。 | |
− | |||
− | |||
− | |||
− | + | 不幸的是,证明程序正确在很大程度上是不切实际的,并且与 Python 软件无关。 即使是微不足道的程序也需要几页长的证明; 中等复杂程序的正确性证明将是巨大的,并且您每天使用的程序(Python 解释器、您的 XML 解析器、您的 Web 浏览器)很少或没有一个可以被证明是正确的。 即使你写下或生成了一个证明,那么也会有验证这个证明的问题; 也许其中存在错误,并且您错误地认为您已经证明了该程序是正确的。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
第120行: | 第64行: | ||
<div id="modularity" class="section"> | <div id="modularity" class="section"> | ||
− | === | + | === 模块化 === |
− | + | 函数式编程的一个更实际的好处是它迫使您将问题分解成小块。 因此,程序更加模块化。 指定和编写一个只做一件事的小函数比一个执行复杂转换的大函数更容易。 小函数也更易于阅读和检查错误。 | |
− | |||
− | |||
− | |||
− | |||
第132行: | 第72行: | ||
<div id="ease-of-debugging-and-testing" class="section"> | <div id="ease-of-debugging-and-testing" class="section"> | ||
− | === | + | === 易于调试和测试 === |
− | + | 测试和调试函数式程序更容易。 | |
− | + | 调试被简化,因为功能通常很小且明确指定。 当程序不工作时,每个函数都是一个接口点,您可以在其中检查数据是否正确。 您可以查看中间输入和输出以快速隔离导致错误的函数。 | |
− | |||
− | |||
− | |||
− | + | 测试更容易,因为每个功能都是单元测试的潜在主题。 函数不依赖于在运行测试之前需要复制的系统状态; 相反,您只需合成正确的输入,然后检查输出是否符合预期。 | |
− | |||
− | |||
− | |||
第150行: | 第84行: | ||
<div id="composability" class="section"> | <div id="composability" class="section"> | ||
− | === | + | === 可组合性 === |
− | + | 在处理函数式程序时,您将编写许多具有不同输入和输出的函数。 其中一些功能不可避免地专门用于特定应用程序,但其他功能将在各种程序中有用。 例如,采用目录路径并返回目录中所有 XML 文件的函数,或者采用文件名并返回其内容的函数,可以应用于许多不同的情况。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | 随着时间的推移,您将形成一个个人实用程序库。 通常,您会通过在新配置中安排现有函数并编写一些专门用于当前任务的函数来组装新程序。 | |
− | |||
− | |||
第170行: | 第97行: | ||
<span id="functional-howto-iterators"></span> | <span id="functional-howto-iterators"></span> | ||
− | == | + | == 迭代器 == |
− | + | 我将首先研究 Python 语言特性,它是编写函数式程序的重要基础:迭代器。 | |
− | |||
− | + | 迭代器是表示数据流的对象; 此对象一次返回一个元素的数据。 Python 迭代器必须支持名为 [[../../library/stdtypes#iterator|__next__()]] 的方法,该方法不接受任何参数并始终返回流的下一个元素。 如果流中没有更多元素,则 [[../../library/stdtypes#iterator|__next__()]] 必须引发 [[../../library/exceptions#StopIteration|StopIteration]] 异常。 不过,迭代器不一定是有限的; 编写一个产生无限数据流的迭代器是完全合理的。 | |
− | |||
− | [[../../library/stdtypes#iterator| | ||
− | |||
− | [[../../library/stdtypes#iterator| | ||
− | |||
− | |||
− | + | 内置的 [[../../library/functions#iter|iter()]] 函数接受一个任意对象并尝试返回一个迭代器,该迭代器将返回该对象的内容或元素,如果该对象不支持迭代,则会引发 [[../../library/exceptions#TypeError|TypeError]]。 Python 的一些内置数据类型支持迭代,最常见的是列表和字典。 如果您可以获得一个迭代器,则该对象称为 [[../../glossary#term-iterable|iterable]]。 | |
− | |||
− | [[../../library/exceptions#TypeError| | ||
− | |||
− | |||
− | |||
− | + | 您可以手动试验迭代界面: | |
<div class="doctest highlight-default notranslate"> | <div class="doctest highlight-default notranslate"> | ||
第196行: | 第111行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> L = [1, 2, 3] |
− | + | >>> it = iter(L) | |
− | + | >>> it | |
− | + | <...iterator object at ...> | |
− | + | >>> it.__next__() # same as next(it) | |
1 | 1 | ||
− | + | >>> next(it) | |
2 | 2 | ||
− | + | >>> next(it) | |
3 | 3 | ||
− | + | >>> next(it) | |
Traceback (most recent call last): | Traceback (most recent call last): | ||
− | File | + | File "<stdin>", line 1, in <module> |
StopIteration | StopIteration | ||
− | + | >>></syntaxhighlight> | |
</div> | </div> | ||
</div> | </div> | ||
− | Python | + | Python 期望在几个不同的上下文中可迭代对象,最重要的是 [[../../reference/compound_stmts#for|for]] 语句。 在语句 <code>for X in Y</code> 中,Y 必须是迭代器或 [[../../library/functions#iter|iter()]] 可以为其创建迭代器的某个对象。 这两个语句是等价的: |
− | |||
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第224行: | 第136行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">for i in iter(obj): |
print(i) | print(i) | ||
for i in obj: | for i in obj: | ||
− | print(i)</ | + | print(i)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 通过使用 [[../../library/stdtypes#list|list()]] 或 [[../../library/stdtypes#tuple|tuple()]] 构造函数,可以将迭代器具体化为列表或元组: | |
− | [[../../library/stdtypes#tuple| | ||
<div class="doctest highlight-default notranslate"> | <div class="doctest highlight-default notranslate"> | ||
第240行: | 第151行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> L = [1, 2, 3] |
− | + | >>> iterator = iter(L) | |
− | + | >>> t = tuple(iterator) | |
− | + | >>> t | |
− | (1, 2, 3)</ | + | (1, 2, 3)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 序列解包也支持迭代器:如果你知道一个迭代器会返回 N 个元素,你可以将它们解包成一个 N 元组: | |
− | N | ||
<div class="doctest highlight-default notranslate"> | <div class="doctest highlight-default notranslate"> | ||
第256行: | 第166行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> L = [1, 2, 3] |
− | + | >>> iterator = iter(L) | |
− | + | >>> a, b, c = iterator | |
− | + | >>> a, b, c | |
− | (1, 2, 3)</ | + | (1, 2, 3)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | [[../../library/functions#max|max()]] 和 [[../../library/functions#min|min()]] 等内置函数可以采用单个迭代器参数,并返回最大或最小元素。 <code>"in"</code> 和 <code>"not in"</code> 运算符也支持迭代器:如果在迭代器返回的流中找到 X,则 <code>X in iterator</code> 为真。 如果迭代器是无限的,你会遇到明显的问题; [[../../library/functions#max|max()]], [[../../library/functions#min|min()]] 永远不会返回,如果元素 X 永远不会出现在流中,则 <code>"in"</code> 和 <code>"not in"</code> 算子赢了也不回。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | <code>"in"</code> | ||
− | + | 请注意,您只能在迭代器中前进; 无法获取前一个元素、重置迭代器或复制它。 迭代器对象可以选择提供这些附加功能,但迭代器协议仅指定 [[../../library/stdtypes#iterator|__next__()]] 方法。 因此,函数可能会消耗迭代器的所有输出,如果您需要对同一流执行不同的操作,则必须创建一个新的迭代器。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
<div id="data-types-that-support-iterators" class="section"> | <div id="data-types-that-support-iterators" class="section"> | ||
− | === | + | === 支持迭代器的数据类型 === |
− | + | 我们已经看到列表和元组如何支持迭代器。 事实上,任何 Python 序列类型,例如字符串,都会自动支持创建迭代器。 | |
− | |||
− | |||
− | + | 在字典上调用 [[../../library/functions#iter|iter()]] 会返回一个迭代器,该迭代器将遍历字典的键: | |
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第295行: | 第191行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">>>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, |
... 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12} | ... 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12} | ||
− | + | >>> for key in m: | |
... print(key, m[key]) | ... print(key, m[key]) | ||
Jan 1 | Jan 1 | ||
第310行: | 第206行: | ||
Oct 10 | Oct 10 | ||
Nov 11 | Nov 11 | ||
− | Dec 12</ | + | Dec 12</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 请注意,从 Python 3.7 开始,字典迭代顺序保证与插入顺序相同。 在早期版本中,行为未指定并且可能因实现而异。 | |
− | |||
− | |||
− | + | 将 [[../../library/functions#iter|iter()]] 应用于字典总是遍历键,但字典具有返回其他迭代器的方法。 如果要迭代值或键/值对,可以显式调用 [[../../library/stdtypes#dict|values()]] 或 [[../../library/stdtypes#dict|items()]] 方法来获得合适的迭代器。 | |
− | |||
− | |||
− | [[../../library/stdtypes#dict| | ||
− | |||
− | + | [[../../library/stdtypes#dict|dict()]] 构造函数可以接受一个迭代器,它返回一个有限的 <code>(key, value)</code> 元组流: | |
− | |||
<div class="doctest highlight-default notranslate"> | <div class="doctest highlight-default notranslate"> | ||
第332行: | 第221行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> L = [('Italy', 'Rome'), ('France', 'Paris'), ('US', 'Washington DC')] |
− | + | >>> dict(iter(L)) | |
− | {'Italy': 'Rome', 'France': 'Paris', 'US': 'Washington DC'}</ | + | {'Italy': 'Rome', 'France': 'Paris', 'US': 'Washington DC'}</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 文件还通过调用 [[../../library/io#io.TextIOBase|readline()]] 方法来支持迭代,直到文件中没有更多行。 这意味着您可以像这样读取文件的每一行: | |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第347行: | 第234行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">for line in file: |
# do something for each line | # do something for each line | ||
− | ...</ | + | ...</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 集合可以从可迭代对象中获取它们的内容,并让您迭代集合的元素: | |
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第361行: | 第247行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">S = {2, 3, 5, 7, 11, 13} |
for i in S: | for i in S: | ||
− | print(i)</ | + | print(i)</syntaxhighlight> |
</div> | </div> | ||
第374行: | 第260行: | ||
<div id="generator-expressions-and-list-comprehensions" class="section"> | <div id="generator-expressions-and-list-comprehensions" class="section"> | ||
− | == | + | == 生成器表达式和列表推导式 == |
− | + | 迭代器输出的两个常见操作是 1) 对每个元素执行一些操作,2) 选择满足某些条件的元素子集。 例如,给定一个字符串列表,您可能希望从每一行中去除尾随空格或提取包含给定子字符串的所有字符串。 | |
− | |||
− | |||
− | |||
− | |||
− | + | 列表推导式和生成器表达式(简称:“listcomps”和“genexps”)是此类操作的简明符号,借用自函数式编程语言 Haskell (https://www.haskell.org/) . 您可以使用以下代码从字符串流中去除所有空格: | |
− | |||
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第391行: | 第270行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">line_list = [' line 1\n', 'line 2 \n', ...] |
# Generator expression -- returns iterator | # Generator expression -- returns iterator | ||
第397行: | 第276行: | ||
# List comprehension -- returns list | # List comprehension -- returns list | ||
− | stripped_list = [line.strip() for line in line_list]</ | + | stripped_list = [line.strip() for line in line_list]</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 您可以通过添加 <code>"if"</code> 条件来仅选择某些元素: | |
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第408行: | 第287行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">stripped_list = [line.strip() for line in line_list |
− | if line != | + | if line != ""]</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 通过列表理解,你会得到一个 Python 列表; <code>stripped_list</code> 是包含结果行的列表,而不是迭代器。 生成器表达式返回一个迭代器,它根据需要计算值,不需要一次实现所有值。 这意味着如果您正在使用返回无限流或大量数据的迭代器,则列表推导式没有用。 在这些情况下,生成器表达式更可取。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | 生成器表达式用括号(“()”)括起来,列表推导式用方括号(“[]”)括起来。 生成器表达式具有以下形式: | |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第429行: | 第301行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">( expression for expr in sequence1 |
if condition1 | if condition1 | ||
for expr2 in sequence2 | for expr2 in sequence2 | ||
第436行: | 第308行: | ||
if condition3 | if condition3 | ||
for exprN in sequenceN | for exprN in sequenceN | ||
− | if conditionN )</ | + | if conditionN )</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 同样,对于列表理解,只有外括号不同(方括号而不是圆括号)。 | |
− | |||
− | + | 生成的输出的元素将是 <code>expression</code> 的连续值。 <code>if</code> 子句都是可选的; 如果存在,仅当 <code>condition</code> 为真时才会评估 <code>expression</code> 并将其添加到结果中。 | |
− | <code>expression</code> | ||
− | |||
− | + | 生成器表达式总是必须写在括号内,但发出函数调用信号的括号也算在内。 如果您想创建一个将立即传递给函数的迭代器,您可以编写: | |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第456行: | 第323行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">obj_total = sum(obj.count for obj in list_all_objects())</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | <code>for...in</code> 子句包含要迭代的序列。 序列的长度不必相同,因为它们是从左到右迭代的, '''不是''' 并行。 对于 <code>sequence1</code> 中的每个元素,<code>sequence2</code> 从头开始循环。 <code>sequence3</code> 然后为来自 <code>sequence1</code> 和 <code>sequence2</code> 的每个结果元素对循环。 | |
− | |||
− | |||
− | <code>sequence2</code> | ||
− | |||
− | + | 换句话说,列表推导式或生成器表达式等效于以下 Python 代码: | |
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第474行: | 第336行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">for expr1 in sequence1: |
if not (condition1): | if not (condition1): | ||
continue # Skip this element | continue # Skip this element | ||
第486行: | 第348行: | ||
# Output the value of | # Output the value of | ||
− | # the expression.</ | + | # the expression.</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 这意味着当有多个 <code>for...in</code> 子句但没有 <code>if</code> 子句时,结果输出的长度将等于所有序列长度的乘积。 如果您有两个长度为 3 的列表,则输出列表的长度为 9 个元素: | |
− | |||
− | |||
− | |||
<div class="doctest highlight-default notranslate"> | <div class="doctest highlight-default notranslate"> | ||
第500行: | 第359行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> seq1 = 'abc' |
− | + | >>> seq2 = (1, 2, 3) | |
− | + | >>> [(x, y) for x in seq1 for y in seq2] | |
[('a', 1), ('a', 2), ('a', 3), | [('a', 1), ('a', 2), ('a', 3), | ||
('b', 1), ('b', 2), ('b', 3), | ('b', 1), ('b', 2), ('b', 3), | ||
− | ('c', 1), ('c', 2), ('c', 3)]</ | + | ('c', 1), ('c', 2), ('c', 3)]</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 为了避免在 Python 的语法中引入歧义,如果 <code>expression</code> 正在创建一个元组,它必须用括号括起来。 下面的第一个列表理解是一个语法错误,而第二个是正确的: | |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第518行: | 第375行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3"># Syntax error |
[x, y for x in seq1 for y in seq2] | [x, y for x in seq1 for y in seq2] | ||
# Correct | # Correct | ||
− | [(x, y) for x in seq1 for y in seq2]</ | + | [(x, y) for x in seq1 for y in seq2]</syntaxhighlight> |
</div> | </div> | ||
第530行: | 第387行: | ||
<div id="generators" class="section"> | <div id="generators" class="section"> | ||
− | == | + | == 发电机 == |
− | + | 生成器是一类特殊的函数,可以简化编写迭代器的任务。 常规函数计算一个值并返回它,但生成器返回一个迭代器,它返回一个值流。 | |
− | |||
− | |||
− | + | 您无疑熟悉 Python 或 C 中常规函数调用的工作方式。 当您调用一个函数时,它会获得一个私有命名空间,在该命名空间中创建了它的局部变量。 当函数到达<code>return</code>语句时,局部变量被销毁,并将值返回给调用者。 稍后对同一函数的调用会创建一个新的私有命名空间和一组新的局部变量。 但是,如果在退出函数时没有丢弃局部变量呢? 如果您稍后可以恢复它停止的功能怎么办? 这就是生成器提供的; 它们可以被认为是可恢复的功能。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | 这是生成器函数的最简单示例: | |
<div class="doctest highlight-default notranslate"> | <div class="doctest highlight-default notranslate"> | ||
第551行: | 第399行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> def generate_ints(N): |
... for i in range(N): | ... for i in range(N): | ||
− | ... yield i</ | + | ... yield i</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 任何包含 [[../../reference/simple_stmts#yield|yield]] 关键字的函数都是生成器函数; 这是由 Python 的 [[../../glossary#term-bytecode|bytecode]] 编译器检测到的,该编译器因此专门编译该函数。 | |
− | |||
− | |||
− | + | 当您调用生成器函数时,它不会返回单个值; 相反,它返回一个支持迭代器协议的生成器对象。 在执行 <code>yield</code> 表达式时,生成器输出 <code>i</code> 的值,类似于 <code>return</code> 语句。 <code>yield</code> 和 <code>return</code> 语句之间的最大区别在于,在到达 <code>yield</code> 时,生成器的执行状态被挂起并保留局部变量。 在下次调用生成器的 [[../../reference/expressions#generator|__next__()]] 方法时,该函数将继续执行。 | |
− | |||
− | |||
− | <code>return</code> | ||
− | |||
− | |||
− | |||
− | |||
− | + | 以下是 <code>generate_ints()</code> 生成器的示例用法: | |
<div class="doctest highlight-default notranslate"> | <div class="doctest highlight-default notranslate"> | ||
第577行: | 第416行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> gen = generate_ints(3) |
− | + | >>> gen | |
− | + | <generator object generate_ints at ...> | |
− | + | >>> next(gen) | |
0 | 0 | ||
− | + | >>> next(gen) | |
1 | 1 | ||
− | + | >>> next(gen) | |
2 | 2 | ||
− | + | >>> next(gen) | |
Traceback (most recent call last): | Traceback (most recent call last): | ||
− | File | + | File "stdin", line 1, in <module> |
− | File | + | File "stdin", line 2, in generate_ints |
− | StopIteration</ | + | StopIteration</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 你同样可以写 <code>for i in generate_ints(5)</code> 或 <code>a, b, c = generate_ints(3)</code>。 | |
− | + | 在一个生成器函数中,<code>return value</code> 导致 <code>StopIteration(value)</code> 从 [[../../reference/expressions#generator|__next__()]] 方法被引发。 一旦发生这种情况,或者到达函数的底部,值的处理结束并且生成器不能产生任何进一步的值。 | |
− | |||
− | |||
− | |||
− | + | 您可以通过编写自己的类并将生成器的所有局部变量存储为实例变量来手动实现生成器的效果。 例如,可以通过将 <code>self.count</code> 设置为 0 并让 [[../../library/stdtypes#iterator|__next__()]] 方法递增 <code>self.count</code> 并返回它来返回整数列表。 然而,对于一个中等复杂的生成器,编写相应的类可能会更加混乱。 | |
− | |||
− | |||
− | 0 | ||
− | |||
− | |||
− | |||
− | + | Python 库中包含的测试套件 [[#id1|:source:`Lib/test/test_generators.py`]] 包含许多更有趣的示例。 这是一个使用生成器递归实现树的有序遍历的生成器。 | |
− | [ | ||
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第619行: | 第446行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3"># A recursive generator that generates Tree leaves in in-order. |
def inorder(t): | def inorder(t): | ||
if t: | if t: | ||
第628行: | 第455行: | ||
for x in inorder(t.right): | for x in inorder(t.right): | ||
− | yield x</ | + | yield x</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | <code>test_generators.py</code> 中的另外两个示例为 N-Queens 问题(将 N 个皇后放在 NxN 棋盘上,以便没有皇后威胁另一个皇后)和骑士之旅(找到一条将骑士带到每个方格的路线)的解决方案一个 NxN 棋盘,无需两次访问任何方块)。 | |
− | |||
− | |||
− | |||
<div id="passing-values-into-a-generator" class="section"> | <div id="passing-values-into-a-generator" class="section"> | ||
− | === | + | === 将值传递给生成器 === |
− | + | 在 Python 2.4 及更早版本中,生成器只产生输出。 一旦调用生成器的代码来创建迭代器,在恢复执行时就无法将任何新信息传递给函数。 您可以通过让生成器查看全局变量或通过传入一些调用者然后修改的可变对象来组合这种能力,但这些方法是混乱的。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | 在 Python 2.5 中,有一种简单的方法可以将值传递给生成器。 [[../../reference/simple_stmts#yield|yield]] 变成了一个表达式,返回一个可以赋值给变量或以其他方式操作的值: | |
− | [[../../reference/simple_stmts#yield| | ||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第657行: | 第474行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">val = (yield i)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 我建议您 '''始终''' 在使用返回值执行某些操作时在 <code>yield</code> 表达式周围放置括号,如上例所示。 括号并不总是必要的,但总是添加它们更容易,而不必记住何时需要它们。 | |
− | |||
− | |||
− | |||
− | + | (<span id="index-0" class="target"></span>[https://www.python.org/dev/peps/pep-0342 PEP 342] 解释了确切的规则,即 <code>yield</code> 表达式必须始终用括号括起来,除非它出现在右侧的顶级表达式中-作业的手边。 这意味着您可以编写 <code>val = yield i</code>,但在进行操作时必须使用括号,如 <code>val = (yield i) + 12</code>。) | |
− | |||
− | |||
− | |||
− | + | 通过调用其 [[../../reference/expressions#generator|send(value)]] 方法将值发送到生成器。 此方法恢复生成器的代码并且 <code>yield</code> 表达式返回指定的值。 如果调用常规的 [[../../reference/expressions#generator|__next__()]] 方法,则 <code>yield</code> 返回 <code>None</code>。 | |
− | <code>yield</code> | ||
− | [[../../reference/expressions#generator| | ||
− | + | 这是一个简单的计数器,递增 1 并允许更改内部计数器的值。 | |
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第683行: | 第491行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">def counter(maximum): |
i = 0 | i = 0 | ||
− | while i | + | while i < maximum: |
val = (yield i) | val = (yield i) | ||
# If value provided, change counter | # If value provided, change counter | ||
第691行: | 第499行: | ||
i = val | i = val | ||
else: | else: | ||
− | i += 1</ | + | i += 1</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 这是更改计数器的示例: | |
<div class="doctest highlight-default notranslate"> | <div class="doctest highlight-default notranslate"> | ||
第702行: | 第510行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> it = counter(10) |
− | + | >>> next(it) | |
0 | 0 | ||
− | + | >>> next(it) | |
1 | 1 | ||
− | + | >>> it.send(8) | |
8 | 8 | ||
− | + | >>> next(it) | |
9 | 9 | ||
− | + | >>> next(it) | |
Traceback (most recent call last): | Traceback (most recent call last): | ||
− | File | + | File "t.py", line 15, in <module> |
it.next() | it.next() | ||
− | StopIteration</ | + | StopIteration</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 因为 <code>yield</code> 经常会返回 <code>None</code>,所以你应该经常检查这种情况。 不要只在表达式中使用它的值,除非您确定 [[../../reference/expressions#generator|send()]] 方法将是用于恢复生成器函数的唯一方法。 | |
− | |||
− | [[../../reference/expressions#generator| | ||
− | |||
− | + | 除了 [[../../reference/expressions#generator|send()]] 之外,生成器还有另外两种方法: | |
− | |||
<ul> | <ul> | ||
− | <li><p>[[../../reference/expressions#generator| | + | <li><p>[[../../reference/expressions#generator|throw(type, value=None, traceback=None)]] 用于在生成器内部引发异常; 异常由 <code>yield</code> 表达式引发,其中生成器的执行暂停。</p></li> |
− | + | <li><p>[[../../reference/expressions#generator|close()]] 在生成器内部引发 [[../../library/exceptions#GeneratorExit|GeneratorExit]] 异常以终止迭代。 收到此异常时,生成器的代码必须引发 [[../../library/exceptions#GeneratorExit|GeneratorExit]] 或 [[../../library/exceptions#StopIteration|StopIteration]]; 捕获异常并执行其他任何操作都是非法的,并且会触发 [[../../library/exceptions#RuntimeError|RuntimeError]]。 [[../../reference/expressions#generator|close()]] 在生成器被垃圾回收时也会被 Python 的垃圾回收器调用。</p> | |
− | <code>yield</code> | + | <p>如果您需要在发生 [[../../library/exceptions#GeneratorExit|GeneratorExit]] 时运行清理代码,我建议使用 <code>try: ... finally:</code> 套件而不是捕获 [[../../library/exceptions#GeneratorExit|GeneratorExit]]。</p></li></ul> |
− | <li><p>[[../../reference/expressions#generator| | ||
− | |||
− | |||
− | [[../../library/exceptions#StopIteration| | ||
− | |||
− | |||
− | |||
− | <p> | ||
− | |||
− | + | 这些变化的累积效应是将生成者从信息的单向生产者转变为生产者和消费者。 | |
− | |||
− | + | 生成器也变成了 '''协程''' ,一种更通用的子程序形式。 子程序在一个点进入并在另一个点退出(函数的顶部,和一个 <code>return</code> 语句),但是协程可以在许多不同的点进入、退出和恢复(<code>yield</code> ] 语句)。 | |
− | |||
− | |||
− | |||
第756行: | 第547行: | ||
<div id="built-in-functions" class="section"> | <div id="built-in-functions" class="section"> | ||
− | == | + | == 内置功能 == |
− | + | 让我们更详细地了解经常与迭代器一起使用的内置函数。 | |
− | + | Python 的两个内置函数 [[../../library/functions#map|map()]] 和 [[../../library/functions#filter|filter()]] 复制了生成器表达式的特征: | |
− | |||
<dl> | <dl> | ||
− | <dt>[[../../library/functions#map| | + | <dt>[[../../library/functions#map|map(f, iterA, iterB, ...)]] 返回序列上的迭代器</dt> |
− | <dd><p><code>f(iterA[0], iterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2]), ...</code> | + | <dd><p><code>f(iterA[0], iterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2]), ...</code>。</p> |
<div class="doctest highlight-default notranslate"> | <div class="doctest highlight-default notranslate"> | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> def upper(s): |
− | ... return s.upper()</ | + | ... return s.upper()</syntaxhighlight> |
</div> | </div> | ||
第780行: | 第570行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> list(map(upper, ['sentence', 'fragment'])) |
['SENTENCE', 'FRAGMENT'] | ['SENTENCE', 'FRAGMENT'] | ||
− | + | >>> [upper(s) for s in ['sentence', 'fragment']] | |
− | ['SENTENCE', 'FRAGMENT']</ | + | ['SENTENCE', 'FRAGMENT']</syntaxhighlight> |
</div> | </div> | ||
第789行: | 第579行: | ||
</div></dd></dl> | </div></dd></dl> | ||
− | + | 您当然可以使用列表理解来达到相同的效果。 | |
− | [[../../library/functions#filter| | + | [[../../library/functions#filter|filter(predicate, iter)]] 返回一个遍历所有满足特定条件的序列元素的迭代器,同样被列表推导式复制。 '''predicate''' 是一个返回某个条件真值的函数; 要与 [[../../library/functions#filter|filter()]] 一起使用,谓词必须采用单个值。 |
− | |||
− | |||
− | |||
− | |||
<div class="doctest highlight-default notranslate"> | <div class="doctest highlight-default notranslate"> | ||
第801行: | 第587行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> def is_even(x): |
− | ... return (x % 2) == 0</ | + | ... return (x % 2) == 0</syntaxhighlight> |
</div> | </div> | ||
第811行: | 第597行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> list(filter(is_even, range(10))) |
− | [0, 2, 4, 6, 8]</ | + | [0, 2, 4, 6, 8]</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 这也可以写成列表理解: | |
<div class="doctest highlight-default notranslate"> | <div class="doctest highlight-default notranslate"> | ||
第823行: | 第609行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> list(x for x in range(10) if is_even(x)) |
− | [0, 2, 4, 6, 8]</ | + | [0, 2, 4, 6, 8]</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | [[../../library/functions#enumerate| | + | [[../../library/functions#enumerate|enumerate(iter, start=0)]] 对迭代中的元素进行计数,返回包含计数(来自 ''start'')和每个元素的 2 元组。 |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第837行: | 第621行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">>>> for item in enumerate(['subject', 'verb', 'object']): |
... print(item) | ... print(item) | ||
(0, 'subject') | (0, 'subject') | ||
(1, 'verb') | (1, 'verb') | ||
− | (2, 'object')</ | + | (2, 'object')</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | [[../../library/functions#enumerate| | + | [[../../library/functions#enumerate|enumerate()]] 在循环遍历列表并记录满足特定条件的索引时经常使用: |
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第853行: | 第636行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">f = open('data.txt', 'r') |
for i, line in enumerate(f): | for i, line in enumerate(f): | ||
if line.strip() == '': | if line.strip() == '': | ||
− | print('Blank line at line #%i' % i)</ | + | print('Blank line at line #%i' % i)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | [[../../library/functions#sorted| | + | [[../../library/functions#sorted|sorted(iterable, key=None, reverse=False)]] 将iterable的所有元素收集到一个列表中,对列表进行排序,返回排序后的结果。 ''key'' 和 ''reverse'' 参数被传递到构造列表的 [[../../library/stdtypes#list|sort()]] 方法。 |
− | |||
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第870行: | 第650行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">>>> import random |
− | + | >>> # Generate 8 random numbers between [0, 10000) | |
− | + | >>> rand_list = random.sample(range(10000), 8) | |
− | + | >>> rand_list | |
[769, 7953, 9828, 6431, 8442, 9878, 6213, 2207] | [769, 7953, 9828, 6431, 8442, 9878, 6213, 2207] | ||
− | + | >>> sorted(rand_list) | |
[769, 2207, 6213, 6431, 7953, 8442, 9828, 9878] | [769, 2207, 6213, 6431, 7953, 8442, 9828, 9878] | ||
− | + | >>> sorted(rand_list, reverse=True) | |
− | [9878, 9828, 8442, 7953, 6431, 6213, 2207, 769]</ | + | [9878, 9828, 8442, 7953, 6431, 6213, 2207, 769]</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | (有关排序的更详细讨论,请参阅 [[../sorting#sortinghowto|Sorting HOW TO]]。) | |
− | + | [[../../library/functions#any|any(iter)]] 和 [[../../library/functions#all|all(iter)]] 内置函数查看可迭代内容的真值。 [[../../library/functions#any|any()]] 如果迭代中的任何元素为真值,则返回 <code>True</code>,如果所有元素都为真值,则 [[../../library/functions#all|all()]] 返回 <code>True</code>真实值: | |
− | |||
− | |||
− | |||
<div class="doctest highlight-default notranslate"> | <div class="doctest highlight-default notranslate"> | ||
第894行: | 第671行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> any([0, 1, 0]) |
True | True | ||
− | + | >>> any([0, 0, 0]) | |
False | False | ||
− | + | >>> any([1, 1, 1]) | |
True | True | ||
− | + | >>> all([0, 1, 0]) | |
False | False | ||
− | + | >>> all([0, 0, 0]) | |
False | False | ||
− | + | >>> all([1, 1, 1]) | |
− | True</ | + | True</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | [[../../library/functions#zip| | + | [[../../library/functions#zip|zip(iterA, iterB, ...)]] 从每个可迭代对象中取出一个元素并在元组中返回它们: |
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第917行: | 第693行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">zip(['a', 'b', 'c'], (1, 2, 3)) => |
− | ('a', 1), ('b', 2), ('c', 3)</ | + | ('a', 1), ('b', 2), ('c', 3)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 它不会构造内存列表并在返回之前耗尽所有输入迭代器; 相反,元组仅在被请求时才被构造和返回。 (这种行为的技术术语是 [https://en.wikipedia.org/wiki/Lazy_evaluation 懒惰评估] 。) | |
− | |||
− | |||
− | + | 此迭代器旨在与长度相同的可迭代对象一起使用。 如果可迭代对象的长度不同,则生成的流将与最短的可迭代对象长度相同。 | |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第935行: | 第707行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">zip(['a', 'b'], (1, 2, 3)) => |
− | ('a', 1), ('b', 2)</ | + | ('a', 1), ('b', 2)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 但是,您应该避免这样做,因为元素可能会从较长的迭代器中取出并被丢弃。 这意味着您不能继续使用迭代器,因为您可能会跳过丢弃的元素。 | |
− | |||
− | |||
第949行: | 第719行: | ||
<div id="the-itertools-module" class="section"> | <div id="the-itertools-module" class="section"> | ||
− | == | + | == itertools 模块 == |
− | + | [[../../library/itertools#module-itertools|itertools]] 模块包含许多常用的迭代器以及用于组合多个迭代器的函数。 本节将通过展示小例子来介绍模块的内容。 | |
− | |||
− | |||
− | + | 该模块的功能分为几个大类: | |
− | * | + | * 基于现有迭代器创建新迭代器的函数。 |
− | * | + | * 将迭代器的元素视为函数参数的函数。 |
− | * | + | * 用于选择迭代器输出部分的函数。 |
− | * | + | * 用于对迭代器的输出进行分组的函数。 |
<div id="creating-new-iterators" class="section"> | <div id="creating-new-iterators" class="section"> | ||
− | === | + | === 创建新的迭代器 === |
− | [[../../library/itertools#itertools| | + | [[../../library/itertools#itertools|itertools.count(start, step)]] 返回均匀间隔值的无限流。 您可以选择提供起始编号(默认为 0)和数字之间的间隔(默认为 1): |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第974行: | 第740行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">itertools.count() => |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... | ||
− | itertools.count(10) = | + | itertools.count(10) => |
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ... | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ... | ||
− | itertools.count(10, 5) = | + | itertools.count(10, 5) => |
− | 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, ...</ | + | 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, ...</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | [[../../library/itertools#itertools| | + | [[../../library/itertools#itertools|itertools.cycle(iter)]] 保存提供的可迭代内容的副本并返回一个新的迭代器,该迭代器从头到尾返回其元素。 新的迭代器将无限重复这些元素。 |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第992行: | 第756行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">itertools.cycle([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, ...</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | [[../library/itertools.html#itertools.repeat| | + | [[../library/itertools.html#itertools.repeat|itertools.repeat(elem, [n])]] 返回提供的元素 ''n'' 次,或者如果没有提供 ''n'' 则无限返回元素。 |
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,005行: | 第768行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">itertools.repeat('abc') => |
abc, abc, abc, abc, abc, abc, abc, abc, abc, abc, ... | abc, abc, abc, abc, abc, abc, abc, abc, abc, abc, ... | ||
− | itertools.repeat('abc', 5) = | + | itertools.repeat('abc', 5) => |
− | abc, abc, abc, abc, abc</ | + | abc, abc, abc, abc, abc</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | [[../../library/itertools#itertools| | + | [[../../library/itertools#itertools|itertools.chain(iterA, iterB, ...)]] 将任意数量的迭代器作为输入,并返回第一个迭代器的所有元素,然后是第二个迭代器的所有元素,依此类推,直到所有可迭代对象都已用尽。 |
− | |||
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,022行: | 第782行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">itertools.chain(['a', 'b', 'c'], (1, 2, 3)) => |
− | a, b, c, 1, 2, 3</ | + | a, b, c, 1, 2, 3</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | [[../library/itertools.html#itertools.islice| | + | [[../library/itertools.html#itertools.islice|itertools.islice(iter, [start], stop, [step])]] 返回一个流,它是迭代器的一个切片。 使用单个 ''stop'' 参数,它将返回第一个 ''stop'' 元素。 如果您提供起始索引,您将获得 ''stop-start'' 元素,如果您为 ''step'' 提供值,则将相应地跳过元素。 与 Python 的字符串和列表切片不同,您不能对 ''start''、''stop'' 或 ''step'' 使用负值。 |
− | |||
− | |||
− | |||
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,039行: | 第794行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">itertools.islice(range(10), 8) => |
0, 1, 2, 3, 4, 5, 6, 7 | 0, 1, 2, 3, 4, 5, 6, 7 | ||
− | itertools.islice(range(10), 2, 8) = | + | itertools.islice(range(10), 2, 8) => |
2, 3, 4, 5, 6, 7 | 2, 3, 4, 5, 6, 7 | ||
− | itertools.islice(range(10), 2, 8, 2) = | + | itertools.islice(range(10), 2, 8, 2) => |
− | 2, 4, 6</ | + | 2, 4, 6</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | [[../library/itertools.html#itertools.tee| | + | [[../library/itertools.html#itertools.tee|itertools.tee(iter, [n])]] 复制一个迭代器; 它返回 ''n'' 个独立迭代器,这些迭代器都将返回源迭代器的内容。 如果您不为 ''n'' 提供值,则默认值为 2。 复制迭代器需要保存源迭代器的一些内容,因此如果迭代器很大并且新迭代器之一比其他迭代器消耗更多,这会消耗大量内存。 |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,061行: | 第810行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">itertools.tee( itertools.count() ) => |
iterA, iterB | iterA, iterB | ||
− | where iterA - | + | where iterA -> |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... | ||
− | and iterB - | + | and iterB -> |
− | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...</ | + | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...</syntaxhighlight> |
</div> | </div> | ||
第1,077行: | 第826行: | ||
<div id="calling-functions-on-elements" class="section"> | <div id="calling-functions-on-elements" class="section"> | ||
− | === | + | === 在元素上调用函数 === |
− | + | [[../../library/operator#module-operator|operator]] 模块包含一组对应于 Python 运算符的函数。 一些示例是 [[../../library/operator#operator|operator.add(a, b)]](添加两个值)、[[../../library/operator#operator|operator.ne(a, b)]](与 <code>a != b</code> 相同)和 [ X117X]operator.attrgetter('id')(返回一个获取 <code>.id</code> 属性的可调用对象)。 | |
− | |||
− | |||
− | [ | ||
− | |||
− | [[../../library/itertools#itertools| | + | [[../../library/itertools#itertools|itertools.starmap(func, iter)]] 假设可迭代对象将返回一个元组流,并使用这些元组作为参数调用 ''func'': |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,093行: | 第836行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">itertools.starmap(os.path.join, |
[('/bin', 'python'), ('/usr', 'bin', 'java'), | [('/bin', 'python'), ('/usr', 'bin', 'java'), | ||
('/usr', 'bin', 'perl'), ('/usr', 'bin', 'ruby')]) | ('/usr', 'bin', 'perl'), ('/usr', 'bin', 'ruby')]) | ||
− | = | + | => |
− | /bin/python, /usr/bin/java, /usr/bin/perl, /usr/bin/ruby</ | + | /bin/python, /usr/bin/java, /usr/bin/perl, /usr/bin/ruby</syntaxhighlight> |
</div> | </div> | ||
第1,106行: | 第849行: | ||
<div id="selecting-elements" class="section"> | <div id="selecting-elements" class="section"> | ||
− | === | + | === 选择元素 === |
− | + | 另一组函数根据谓词选择迭代器元素的子集。 | |
− | |||
− | [[../../library/itertools#itertools| | + | [[../../library/itertools#itertools|itertools.filterfalse(predicate, iter)]] 与 [[../../library/functions#filter|filter()]] 相反,返回谓词返回 false 的所有元素: |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,119行: | 第859行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">itertools.filterfalse(is_even, itertools.count()) => |
− | 1, 3, 5, 7, 9, 11, 13, 15, ...</ | + | 1, 3, 5, 7, 9, 11, 13, 15, ...</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | [[../../library/itertools#itertools| | + | [[../../library/itertools#itertools|itertools.takewhile(predicate, iter)]] 返回元素,只要谓词返回 true。 一旦谓词返回 false,迭代器将发出结果结束的信号。 |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,133行: | 第871行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">def less_than_10(x): |
− | return x | + | return x < 10 |
− | itertools.takewhile(less_than_10, itertools.count()) = | + | itertools.takewhile(less_than_10, itertools.count()) => |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 | ||
− | itertools.takewhile(is_even, itertools.count()) = | + | itertools.takewhile(is_even, itertools.count()) => |
− | 0</ | + | 0</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | [[../../library/itertools#itertools| | + | [[../../library/itertools#itertools|itertools.dropwhile(predicate, iter)]] 在谓词返回真时丢弃元素,然后返回可迭代结果的其余部分。 |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,153行: | 第889行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">itertools.dropwhile(less_than_10, itertools.count()) => |
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ... | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ... | ||
− | itertools.dropwhile(is_even, itertools.count()) = | + | itertools.dropwhile(is_even, itertools.count()) => |
− | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...</ | + | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | [[../../library/itertools#itertools| | + | [[../../library/itertools#itertools|itertools.compress(data, selectors)]] 接受两个迭代器并只返回 ''data'' 中 ''selectors'' 的对应元素为真的那些元素,只要有一个就停止筋疲力尽: |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,170行: | 第904行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">itertools.compress([1, 2, 3, 4, 5], [True, True, False, False, True]) => |
− | 1, 2, 5</ | + | 1, 2, 5</syntaxhighlight> |
</div> | </div> | ||
第1,180行: | 第914行: | ||
<div id="combinatoric-functions" class="section"> | <div id="combinatoric-functions" class="section"> | ||
− | === | + | === 组合函数 === |
− | + | [[../../library/itertools#itertools|itertools.combinations(iterable, r)]] 返回一个迭代器,给出 ''iterable'' 中包含的元素的所有可能的 ''r'' 元组组合。 | |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,190行: | 第922行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">itertools.combinations([1, 2, 3, 4, 5], 2) => |
(1, 2), (1, 3), (1, 4), (1, 5), | (1, 2), (1, 3), (1, 4), (1, 5), | ||
(2, 3), (2, 4), (2, 5), | (2, 3), (2, 4), (2, 5), | ||
第1,196行: | 第928行: | ||
(4, 5) | (4, 5) | ||
− | itertools.combinations([1, 2, 3, 4, 5], 3) = | + | itertools.combinations([1, 2, 3, 4, 5], 3) => |
(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), (1, 4, 5), | (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), (1, 4, 5), | ||
(2, 3, 4), (2, 3, 5), (2, 4, 5), | (2, 3, 4), (2, 3, 5), (2, 4, 5), | ||
− | (3, 4, 5)</ | + | (3, 4, 5)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 每个元组中的元素保持与 ''iterable'' 返回它们相同的顺序。 例如,在上述示例中,数字 1 始终位于 2、3、4 或 5 之前。 一个类似的函数,[[../../library/itertools#itertools|itertools.permutations(iterable, r=None)]],移除了这个对顺序的限制,返回所有可能的长度为 ''r'' 的排列: | |
− | ''iterable'' | ||
− | |||
− | [[../../library/itertools#itertools| | ||
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,215行: | 第942行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">itertools.permutations([1, 2, 3, 4, 5], 2) => |
(1, 2), (1, 3), (1, 4), (1, 5), | (1, 2), (1, 3), (1, 4), (1, 5), | ||
(2, 1), (2, 3), (2, 4), (2, 5), | (2, 1), (2, 3), (2, 4), (2, 5), | ||
第1,222行: | 第949行: | ||
(5, 1), (5, 2), (5, 3), (5, 4) | (5, 1), (5, 2), (5, 3), (5, 4) | ||
− | itertools.permutations([1, 2, 3, 4, 5]) = | + | itertools.permutations([1, 2, 3, 4, 5]) => |
(1, 2, 3, 4, 5), (1, 2, 3, 5, 4), (1, 2, 4, 3, 5), | (1, 2, 3, 4, 5), (1, 2, 3, 5, 4), (1, 2, 4, 3, 5), | ||
... | ... | ||
− | (5, 4, 3, 2, 1)</ | + | (5, 4, 3, 2, 1)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 如果您不为 ''r'' 提供值,则使用可迭代的长度,这意味着所有元素都被置换。 | |
− | |||
− | + | 请注意,这些函数按位置生成所有可能的组合,并且不需要 ''iterable'' 的内容是唯一的: | |
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,240行: | 第965行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">itertools.permutations('aba', 3) => |
('a', 'b', 'a'), ('a', 'a', 'b'), ('b', 'a', 'a'), | ('a', 'b', 'a'), ('a', 'a', 'b'), ('b', 'a', 'a'), | ||
− | ('b', 'a', 'a'), ('a', 'a', 'b'), ('a', 'b', 'a')</ | + | ('b', 'a', 'a'), ('a', 'a', 'b'), ('a', 'b', 'a')</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 相同的元组 <code>('a', 'a', 'b')</code> 出现了两次,但两个 'a' 字符串来自不同的位置。 | |
− | |||
− | + | [[../../library/itertools#itertools|itertools.combinations_with_replacement(iterable, r)]] 函数放宽了不同的约束:元素可以在单个元组中重复。 从概念上讲,为每个元组的第一个位置选择一个元素,然后在选择第二个元素之前替换它。 | |
− | |||
− | |||
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,260行: | 第980行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">itertools.combinations_with_replacement([1, 2, 3, 4, 5], 2) => |
(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), | (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), | ||
(2, 2), (2, 3), (2, 4), (2, 5), | (2, 2), (2, 3), (2, 4), (2, 5), | ||
(3, 3), (3, 4), (3, 5), | (3, 3), (3, 4), (3, 5), | ||
(4, 4), (4, 5), | (4, 4), (4, 5), | ||
− | (5, 5)</ | + | (5, 5)</syntaxhighlight> |
</div> | </div> | ||
第1,274行: | 第994行: | ||
<div id="grouping-elements" class="section"> | <div id="grouping-elements" class="section"> | ||
− | === | + | === 分组元素 === |
− | + | 我将讨论的最后一个函数 [[../../library/itertools#itertools|itertools.groupby(iter, key_func=None)]] 是最复杂的。 <code>key_func(elem)</code> 是一个函数,可以为迭代返回的每个元素计算一个键值。 如果您不提供键函数,键就是每个元素本身。 | |
− | |||
− | |||
− | [[../../library/itertools#itertools| | + | [[../../library/itertools#itertools|groupby()]] 从底层迭代中收集所有具有相同键值的连续元素,并返回一个包含键值和具有该键的元素的迭代器的 2 元组流。 |
− | |||
− | 2 | ||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,288行: | 第1,004行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">city_list = [('Decatur', 'AL'), ('Huntsville', 'AL'), ('Selma', 'AL'), |
('Anchorage', 'AK'), ('Nome', 'AK'), | ('Anchorage', 'AK'), ('Nome', 'AK'), | ||
('Flagstaff', 'AZ'), ('Phoenix', 'AZ'), ('Tucson', 'AZ'), | ('Flagstaff', 'AZ'), ('Phoenix', 'AZ'), ('Tucson', 'AZ'), | ||
第1,297行: | 第1,013行: | ||
return city_state[1] | return city_state[1] | ||
− | itertools.groupby(city_list, get_state) = | + | itertools.groupby(city_list, get_state) => |
('AL', iterator-1), | ('AL', iterator-1), | ||
('AK', iterator-2), | ('AK', iterator-2), | ||
第1,303行: | 第1,019行: | ||
where | where | ||
− | iterator-1 = | + | iterator-1 => |
('Decatur', 'AL'), ('Huntsville', 'AL'), ('Selma', 'AL') | ('Decatur', 'AL'), ('Huntsville', 'AL'), ('Selma', 'AL') | ||
− | iterator-2 = | + | iterator-2 => |
('Anchorage', 'AK'), ('Nome', 'AK') | ('Anchorage', 'AK'), ('Nome', 'AK') | ||
− | iterator-3 = | + | iterator-3 => |
− | ('Flagstaff', 'AZ'), ('Phoenix', 'AZ'), ('Tucson', 'AZ')</ | + | ('Flagstaff', 'AZ'), ('Phoenix', 'AZ'), ('Tucson', 'AZ')</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | [[../../library/itertools#itertools| | + | [[../../library/itertools#itertools|groupby()]] 假设底层迭代的内容已经根据键进行了排序。 请注意,返回的迭代器也使用底层迭代器,因此您必须在请求 iterator-2 及其对应的 key 之前使用 iterator-1 的结果。 |
− | |||
− | |||
− | |||
第1,324行: | 第1,037行: | ||
<div id="the-functools-module" class="section"> | <div id="the-functools-module" class="section"> | ||
− | == | + | == 功能工具模块 == |
− | + | Python 2.5 中的 [[../../library/functools#module-functools|functools]] 模块包含一些高阶函数。 '''高阶函数''' 将一个或多个函数作为输入并返回一个新函数。 该模块中最有用的工具是 [[../../library/functools#functools|functools.partial()]] 函数。 | |
− | |||
− | |||
− | [[../../library/functools#functools| | ||
− | + | 对于以函数式风格编写的程序,您有时会希望构建填充了一些参数的现有函数的变体。 考虑一个 Python 函数 <code>f(a, b, c)</code>; 你可能希望创建一个新的函数 <code>g(b, c)</code> 相当于 <code>f(1, b, c)</code>; 您正在为 <code>f()</code> 的参数之一填充值。 这称为“部分功能应用”。 | |
− | |||
− | |||
− | <code>g(b, c)</code> | ||
− | |||
− | + | [[../../library/functools#functools|partial()]] 的构造函数采用参数 <code>(function, arg1, arg2, ..., kwarg1=value1, kwarg2=value2)</code>。 结果对象是可调用的,因此您可以调用它来使用填充的参数调用 <code>function</code>。 | |
− | <code>(function, arg1, arg2, ..., kwarg1=value1, kwarg2=value2)</code> | ||
− | |||
− | |||
− | + | 这是一个小而现实的例子: | |
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,348行: | 第1,051行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">import functools |
def log(message, subsystem): | def log(message, subsystem): | ||
− | + | """Write the contents of 'message' to the specified subsystem.""" | |
print('%s: %s' % (subsystem, message)) | print('%s: %s' % (subsystem, message)) | ||
... | ... | ||
server_log = functools.partial(log, subsystem='server') | server_log = functools.partial(log, subsystem='server') | ||
− | server_log('Unable to open socket')</ | + | server_log('Unable to open socket')</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | [[../library/functools.html#functools.reduce| | + | [[../library/functools.html#functools.reduce|functools.reduce(func, iter, [initial_value])]] 累积对所有可迭代元素执行操作,因此不能应用于无限可迭代元素。 ''func'' 必须是一个接受两个元素并返回一个值的函数。 [[../../library/functools#functools|functools.reduce()]] 取迭代器返回的前两个元素 A 和 B 并计算 <code>func(A, B)</code>。 然后它请求第三个元素 C,计算 <code>func(func(A, B), C)</code>,将此结果与返回的第四个元素组合,并继续直到迭代用完。 如果可迭代对象根本没有返回值,则会引发 [[../../library/exceptions#TypeError|TypeError]] 异常。 如果提供了初始值,则将其用作起点,并且 <code>func(initial_value, A)</code> 是第一个计算。 |
− | |||
− | |||
− | |||
− | |||
− | <code>func(A, B)</code> | ||
− | <code>func(func(A, B), C)</code> | ||
− | |||
− | |||
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,377行: | 第1,070行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">>>> import operator, functools |
− | + | >>> functools.reduce(operator.concat, ['A', 'BB', 'C']) | |
'ABBC' | 'ABBC' | ||
− | + | >>> functools.reduce(operator.concat, []) | |
Traceback (most recent call last): | Traceback (most recent call last): | ||
... | ... | ||
TypeError: reduce() of empty sequence with no initial value | TypeError: reduce() of empty sequence with no initial value | ||
− | + | >>> functools.reduce(operator.mul, [1, 2, 3], 1) | |
6 | 6 | ||
− | + | >>> functools.reduce(operator.mul, [], 1) | |
− | 1</ | + | 1</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 如果您将 [[../../library/operator#operator|operator.add()]] 与 [[../../library/functools#functools|functools.reduce()]] 一起使用,您将把可迭代对象的所有元素相加。 这种情况非常常见,以至于有一个特殊的内置函数 [[../../library/functions#sum|sum()]] 来计算它: | |
− | |||
− | |||
<div class="doctest highlight-default notranslate"> | <div class="doctest highlight-default notranslate"> | ||
第1,400行: | 第1,091行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> import functools, operator |
− | + | >>> functools.reduce(operator.add, [1, 2, 3, 4], 0) | |
10 | 10 | ||
− | + | >>> sum([1, 2, 3, 4]) | |
10 | 10 | ||
− | + | >>> sum([]) | |
− | 0</ | + | 0</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 但是,对于 [[../../library/functools#functools|functools.reduce()]] 的许多用途,只需编写明显的 [[../../reference/compound_stmts#for|for]] 循环会更清晰: | |
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,418行: | 第1,108行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">import functools |
# Instead of: | # Instead of: | ||
product = functools.reduce(operator.mul, [1, 2, 3], 1) | product = functools.reduce(operator.mul, [1, 2, 3], 1) | ||
第1,425行: | 第1,115行: | ||
product = 1 | product = 1 | ||
for i in [1, 2, 3]: | for i in [1, 2, 3]: | ||
− | product *= i</ | + | product *= i</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 一个相关的函数是 [[../../library/itertools#itertools|itertools.accumulate(iterable, func=operator.add)]]。 它执行相同的计算,但不是只返回最终结果,<code>accumulate()</code> 返回一个迭代器,该迭代器也产生每个部分结果: | |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,438行: | 第1,126行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">itertools.accumulate([1, 2, 3, 4, 5]) => |
1, 3, 6, 10, 15 | 1, 3, 6, 10, 15 | ||
− | itertools.accumulate([1, 2, 3, 4, 5], operator.mul) = | + | itertools.accumulate([1, 2, 3, 4, 5], operator.mul) => |
− | 1, 2, 6, 24, 120</ | + | 1, 2, 6, 24, 120</syntaxhighlight> |
</div> | </div> | ||
第1,449行: | 第1,137行: | ||
<div id="the-operator-module" class="section"> | <div id="the-operator-module" class="section"> | ||
− | === | + | === 操作员模块 === |
− | + | [[../../library/operator#module-operator|operator]] 模块前面已经提到过。 它包含一组对应于 Python 操作符的函数。 这些函数在函数式代码中通常很有用,因为它们使您无需编写执行单个操作的琐碎函数。 | |
− | |||
− | |||
− | |||
− | + | 该模块中的一些功能是: | |
− | * | + | * 数学运算:<code>add()</code>、<code>sub()</code>、<code>mul()</code>、<code>floordiv()</code>、<code>abs()</code>、…… |
− | * | + | * 逻辑运算:<code>not_()</code>、<code>truth()</code>。 |
− | * | + | * 按位运算:<code>and_()</code>、<code>or_()</code>、<code>invert()</code>。 |
− | * | + | * 比较:<code>eq()</code>、<code>ne()</code>、<code>lt()</code>、<code>le()</code>、<code>gt()</code>和<code>ge()</code>。 |
− | * | + | * 对象标识:<code>is_()</code>、<code>is_not()</code>。 |
− | + | 有关完整列表,请参阅操作员模块的文档。 | |
第1,472行: | 第1,157行: | ||
<div id="small-functions-and-the-lambda-expression" class="section"> | <div id="small-functions-and-the-lambda-expression" class="section"> | ||
− | == | + | == 小函数和 lambda 表达式 == |
− | + | 在编写函数式程序时,您通常需要一些充当谓词或以某种方式组合元素的函数。 | |
− | |||
− | + | 如果有合适的 Python 内置函数或模块函数,则根本不需要定义新函数: | |
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,484行: | 第1,167行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">stripped_lines = [line.strip() for line in lines] |
− | existing_files = filter(os.path.exists, file_list)</ | + | existing_files = filter(os.path.exists, file_list)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 如果您需要的函数不存在,则需要编写它。 编写小函数的一种方法是使用 [[../../reference/expressions#lambda|lambda]] 表达式。 <code>lambda</code> 接受多个参数和组合这些参数的表达式,并创建一个返回表达式值的匿名函数: | |
− | |||
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,499行: | 第1,179行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">adder = lambda x, y: x+y |
− | print_assign = lambda name, value: name + '=' + str(value)</ | + | print_assign = lambda name, value: name + '=' + str(value)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 另一种方法是使用 <code>def</code> 语句并以通常的方式定义函数: | |
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,513行: | 第1,192行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">def adder(x, y): |
return x + y | return x + y | ||
def print_assign(name, value): | def print_assign(name, value): | ||
− | return name + '=' + str(value)</ | + | return name + '=' + str(value)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 哪个替代方案更可取? 这是一个风格问题; 我通常的做法是避免使用 <code>lambda</code>。 | |
− | |||
− | + | 我偏爱的原因之一是 <code>lambda</code> 在它可以定义的功能方面非常有限。 结果必须可以作为单个表达式进行计算,这意味着您不能进行多路 <code>if... elif... else</code> 比较或 <code>try... except</code> 语句。 如果您试图在 <code>lambda</code> 语句中做太多事情,最终会得到一个难以阅读的过于复杂的表达式。 快,下面的代码在做什么? | |
− | |||
− | |||
− | |||
− | <code>lambda</code> | ||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,536行: | 第1,209行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">import functools |
− | total = functools.reduce(lambda a, b: (0, a[1] + b[1]), items)[1]</ | + | total = functools.reduce(lambda a, b: (0, a[1] + b[1]), items)[1]</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 您可以弄清楚,但需要时间来理清表达式以弄清楚发生了什么。 使用简短的嵌套 <code>def</code> 语句会使事情变得更好: | |
− | |||
− | |||
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,550行: | 第1,221行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">import functools |
def combine(a, b): | def combine(a, b): | ||
return 0, a[1] + b[1] | return 0, a[1] + b[1] | ||
− | total = functools.reduce(combine, items)[1]</ | + | total = functools.reduce(combine, items)[1]</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 但如果我只是使用 <code>for</code> 循环,那将是最好的: | |
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,565行: | 第1,236行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">total = 0 |
for a, b in items: | for a, b in items: | ||
− | total += b</ | + | total += b</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 或者 [[../../library/functions#sum|sum()]] 内置和生成器表达式: | |
<div class="highlight-python3 notranslate"> | <div class="highlight-python3 notranslate"> | ||
第1,578行: | 第1,249行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python3">total = sum(b for a, b in items)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | [[../../library/functools#functools|functools.reduce()]] 的许多用途在编写为 <code>for</code> 循环时更加清晰。 | |
− | Fredrik Lundh | + | Fredrik Lundh 曾经建议使用以下规则来重构 <code>lambda</code> 的使用: |
− | <code>lambda</code> | ||
− | # | + | # 编写一个 lambda 函数。 |
− | # | + | # 写一条评论来解释 lambda 的作用。 |
− | # | + | # 研究一下评论,然后想出一个能抓住评论本质的名字。 |
− | # | + | # 使用该名称将 lambda 转换为 def 语句。 |
− | # | + | # 删除评论。 |
− | + | 我真的很喜欢这些规则,但你可以自由地不同意这种无 lambda 的风格是否更好。 | |
− | |||
第1,601行: | 第1,270行: | ||
<div id="revision-history-and-acknowledgements" class="section"> | <div id="revision-history-and-acknowledgements" class="section"> | ||
− | == | + | == 修订历史和致谢 == |
− | + | 作者要感谢以下人员对本文的各种草稿提供建议、更正和帮助:Ian Bicking、Nick Coghlan、Nick Efford、Raymond Hettinger、Jim Jewett、Mike Krell、Leandro Lameiro、Jussi Salmela、Collin Winter,布莱克温顿。 | |
− | |||
− | |||
− | |||
− | + | 0.1 版:2006 年 6 月 30 日发布。 | |
− | + | 0.11 版:2006 年 7 月 1 日发布。 错别字修复。 | |
− | + | 0.2 版:2006 年 7 月 10 日发布。 将 genexp 和 listcomp 部分合并为一个。 错别字修复。 | |
− | |||
− | + | 0.21 版:在导师邮件列表中添加了更多建议参考。 | |
− | + | 0.30 版本:在 Collin Winter 编写的 <code>functional</code> 模块上添加了一个部分; 在操作员模块上添加了简短的部分; 其他一些编辑。 | |
− | |||
第1,624行: | 第1,288行: | ||
<div id="references" class="section"> | <div id="references" class="section"> | ||
− | == | + | == 参考 == |
<div id="general" class="section"> | <div id="general" class="section"> | ||
− | === | + | === 一般 === |
− | ''' | + | '''计算机程序的结构和解释''' ,Harold Abelson 和 Gerald Jay Sussman 与 Julie Sussman 合着。 全文在 https://mitpress.mit.edu/sicp/。 在这本经典的计算机科学教科书中,第 2 章和第 3 章讨论了使用序列和流来组织程序内的数据流。 本书使用 Scheme 作为示例,但这些章节中描述的许多设计方法都适用于函数式 Python 代码。 |
− | Gerald Jay Sussman | ||
− | https://mitpress.mit.edu/sicp/ | ||
− | |||
− | |||
− | |||
− | Python | ||
− | http://www.defmacro.org/ramblings/fp. | + | http://www.defmacro.org/ramblings/fp.html:函数式编程的一般介绍,使用 Java 示例,并有冗长的历史介绍。 |
− | |||
− | https://en.wikipedia.org/wiki/ | + | https://en.wikipedia.org/wiki/Functional_programming:描述函数式编程的一般维基百科条目。 |
− | |||
− | https://en.wikipedia.org/wiki/ | + | https://en.wikipedia.org/wiki/Coroutine:协程入口。 |
− | https://en.wikipedia.org/wiki/ | + | https://en.wikipedia.org/wiki/Currying:柯里化概念的入口。 |
第1,652行: | 第1,308行: | ||
<div id="python-specific" class="section"> | <div id="python-specific" class="section"> | ||
− | === Python | + | === 特定于 Python 的 === |
− | http://gnosis.cx/TPiP/ | + | http://gnosis.cx/TPiP/:David Mertz 的书 Text Processing in Python 的第一章讨论了文本处理的函数式编程,在标题为“利用高阶文本处理中的函数”。 |
− | Text Processing in Python | ||
− | |||
− | |||
− | Mertz | + | Mertz 还为 IBM 的 DeveloperWorks 站点撰写了关于函数式编程的 3 部分系列文章; 见[https://developer.ibm.com/articles/l-prog/ 第1部分]、[https://developer.ibm.com/tutorials/l-prog2/ 第2部分]和[https://developer.ibm.com/tutorials/l-prog3/ 第3部分], |
− | |||
− | [https://developer.ibm.com/articles/l-prog/ | ||
− | [https://developer.ibm.com/tutorials/l-prog2/ | ||
− | [https://developer.ibm.com/tutorials/l-prog3/ | ||
第1,669行: | 第1,318行: | ||
<div id="python-documentation" class="section"> | <div id="python-documentation" class="section"> | ||
− | === Python | + | === Python 文档 === |
− | + | [[../../library/itertools#module-itertools|itertools]] 模块的文档。 | |
− | + | [[../../library/functools#module-functools|functools]] 模块的文档。 | |
− | + | [[../../library/operator#module-operator|operator]] 模块的文档。 | |
− | <span id="index-1" class="target"></span>[https://www.python.org/dev/peps/pep-0289 | + | <span id="index-1" class="target"></span>[https://www.python.org/dev/peps/pep-0289 PEP 289]:“生成器表达式” |
− | <span id="index-2" class="target"></span>[https://www.python.org/dev/peps/pep-0342 | + | <span id="index-2" class="target"></span>[https://www.python.org/dev/peps/pep-0342 PEP 342]:“Coroutines via Enhanced Generators”描述了 Python 2.5 中的新生成器特性。 |
− | |||
第1,686行: | 第1,334行: | ||
</div> | </div> | ||
+ | |||
+ | </div> | ||
+ | <div class="clearer"> | ||
+ | |||
+ | |||
</div> | </div> | ||
− | [[Category:Python 3.9 | + | [[Category:Python 3.9 文档]] |
2021年10月31日 (日) 04:50的最新版本
函数式编程HOWTO
- 作者
库克林
- 发布
0.32
在本文档中,我们将了解适合以函数式风格实现程序的 Python 特性。 在介绍了函数式编程的概念之后,我们将看看语言特性,例如 iterators 和 generators 以及相关的库模块,例如 itertools 和functools。
介绍
本节解释函数式编程的基本概念; 如果您只是对了解 Python 语言功能感兴趣,请跳到下一部分 Iterators。
编程语言支持以多种不同方式分解问题:
- 大多数编程语言都是 过程化 :程序是指令列表,告诉计算机如何处理程序的输入。 C、Pascal 甚至 Unix shell 都是过程语言。
- 在 声明性 语言中,您编写描述要解决的问题的规范,语言实现会弄清楚如何有效地执行计算。 SQL 是您最有可能熟悉的声明性语言; SQL 查询描述了您要检索的数据集,SQL 引擎决定是扫描表还是使用索引,应先执行哪些子条款等。
- 面向对象的程序操作对象的集合。 对象具有内部状态并支持以某种方式查询或修改此内部状态的方法。 Smalltalk 和 Java 是面向对象的语言。 C++ 和 Python 是支持面向对象编程的语言,但不强制使用面向对象的特性。
- 函数式 编程将问题分解为一组函数。 理想情况下,函数只接受输入并产生输出,并且没有任何影响给定输入产生的输出的内部状态。 著名的函数式语言包括 ML 系列(标准 ML、OCaml 和其他变体)和 Haskell。
某些计算机语言的设计者选择强调一种特定的编程方法。 这通常会使编写使用不同方法的程序变得困难。 其他语言是支持多种不同方法的多范式语言。 Lisp、C++ 和 Python 是多范式的; 您可以使用所有这些语言编写主要是过程性、面向对象或功能性的程序或库。 在一个大型程序中,可能会使用不同的方法编写不同的部分; 例如,GUI 可能是面向对象的,而处理逻辑是程序性的或功能性的。
在函数式程序中,输入流经一组函数。 每个函数对其输入进行操作并产生一些输出。 函数式风格不鼓励具有副作用的函数,这些函数会修改内部状态或进行在函数返回值中不可见的其他更改。 完全没有副作用的函数称为纯函数。 避免副作用意味着不使用随着程序运行而更新的数据结构; 每个函数的输出必须只依赖于它的输入。
有些语言对纯度非常严格,甚至没有赋值语句,例如 a=3
或 c = a + b
,但很难避免所有副作用,例如打印到屏幕或写入磁盘文件。 另一个例子是调用 print() 或 time.sleep() 函数,这两个函数都没有返回有用的值。 两者都只是因为它们向屏幕发送一些文本或暂停执行一秒钟的副作用而被调用。
以函数式风格编写的 Python 程序通常不会走到避免所有 I/O 或所有分配的极端; 相反,他们将提供一个功能性的界面,但会在内部使用非功能性的特性。 例如,函数的实现仍然会使用对局部变量的赋值,但不会修改全局变量或产生其他副作用。
函数式编程可以被认为是面向对象编程的对立面。 对象是包含一些内部状态以及允许您修改此状态的一组方法调用的小胶囊,程序由进行正确的状态更改集组成。 函数式编程希望尽可能避免状态更改,并处理函数之间的数据流动。 在 Python 中,您可以通过编写接受和返回表示应用程序中的对象(电子邮件消息、事务等)的实例的函数来组合这两种方法。
功能设计似乎是一个奇怪的工作限制。 为什么要避免对象和副作用? 函数式风格具有理论和实践优势:
- 正式的可证明性。
- 模块化。
- 可组合性。
- 易于调试和测试。
形式证明
理论上的好处是更容易构建函数式程序正确的数学证明。
长期以来,研究人员一直对寻找数学证明程序正确的方法感兴趣。 这不同于在大量输入上测试程序并得出其输出通常是正确的结论,或阅读程序的源代码并得出代码看起来正确的结论; 相反,目标是严格证明程序为所有可能的输入产生正确的结果。
用于证明程序正确的技术是写下 不变量 、输入数据和程序变量的属性,这些属性始终为真。 对于每一行代码,你然后证明如果不变量 X 和 Y 为真 before 行被执行,略有不同的不变量 X' 和 Y' 为真 after 行是执行。 这一直持续到程序结束,此时不变量应该与程序输出的所需条件相匹配。
函数式编程避免赋值的出现是因为这种技术很难处理赋值; 赋值可以破坏赋值之前为真的不变量,而不会产生任何可以向前传播的新不变量。
不幸的是,证明程序正确在很大程度上是不切实际的,并且与 Python 软件无关。 即使是微不足道的程序也需要几页长的证明; 中等复杂程序的正确性证明将是巨大的,并且您每天使用的程序(Python 解释器、您的 XML 解析器、您的 Web 浏览器)很少或没有一个可以被证明是正确的。 即使你写下或生成了一个证明,那么也会有验证这个证明的问题; 也许其中存在错误,并且您错误地认为您已经证明了该程序是正确的。
模块化
函数式编程的一个更实际的好处是它迫使您将问题分解成小块。 因此,程序更加模块化。 指定和编写一个只做一件事的小函数比一个执行复杂转换的大函数更容易。 小函数也更易于阅读和检查错误。
易于调试和测试
测试和调试函数式程序更容易。
调试被简化,因为功能通常很小且明确指定。 当程序不工作时,每个函数都是一个接口点,您可以在其中检查数据是否正确。 您可以查看中间输入和输出以快速隔离导致错误的函数。
测试更容易,因为每个功能都是单元测试的潜在主题。 函数不依赖于在运行测试之前需要复制的系统状态; 相反,您只需合成正确的输入,然后检查输出是否符合预期。
可组合性
在处理函数式程序时,您将编写许多具有不同输入和输出的函数。 其中一些功能不可避免地专门用于特定应用程序,但其他功能将在各种程序中有用。 例如,采用目录路径并返回目录中所有 XML 文件的函数,或者采用文件名并返回其内容的函数,可以应用于许多不同的情况。
随着时间的推移,您将形成一个个人实用程序库。 通常,您会通过在新配置中安排现有函数并编写一些专门用于当前任务的函数来组装新程序。
迭代器
我将首先研究 Python 语言特性,它是编写函数式程序的重要基础:迭代器。
迭代器是表示数据流的对象; 此对象一次返回一个元素的数据。 Python 迭代器必须支持名为 __next__() 的方法,该方法不接受任何参数并始终返回流的下一个元素。 如果流中没有更多元素,则 __next__() 必须引发 StopIteration 异常。 不过,迭代器不一定是有限的; 编写一个产生无限数据流的迭代器是完全合理的。
内置的 iter() 函数接受一个任意对象并尝试返回一个迭代器,该迭代器将返回该对象的内容或元素,如果该对象不支持迭代,则会引发 TypeError。 Python 的一些内置数据类型支持迭代,最常见的是列表和字典。 如果您可以获得一个迭代器,则该对象称为 iterable。
您可以手动试验迭代界面:
Python 期望在几个不同的上下文中可迭代对象,最重要的是 for 语句。 在语句 for X in Y
中,Y 必须是迭代器或 iter() 可以为其创建迭代器的某个对象。 这两个语句是等价的:
通过使用 list() 或 tuple() 构造函数,可以将迭代器具体化为列表或元组:
序列解包也支持迭代器:如果你知道一个迭代器会返回 N 个元素,你可以将它们解包成一个 N 元组:
max() 和 min() 等内置函数可以采用单个迭代器参数,并返回最大或最小元素。 "in"
和 "not in"
运算符也支持迭代器:如果在迭代器返回的流中找到 X,则 X in iterator
为真。 如果迭代器是无限的,你会遇到明显的问题; max(), min() 永远不会返回,如果元素 X 永远不会出现在流中,则 "in"
和 "not in"
算子赢了也不回。
请注意,您只能在迭代器中前进; 无法获取前一个元素、重置迭代器或复制它。 迭代器对象可以选择提供这些附加功能,但迭代器协议仅指定 __next__() 方法。 因此,函数可能会消耗迭代器的所有输出,如果您需要对同一流执行不同的操作,则必须创建一个新的迭代器。
支持迭代器的数据类型
我们已经看到列表和元组如何支持迭代器。 事实上,任何 Python 序列类型,例如字符串,都会自动支持创建迭代器。
在字典上调用 iter() 会返回一个迭代器,该迭代器将遍历字典的键:
请注意,从 Python 3.7 开始,字典迭代顺序保证与插入顺序相同。 在早期版本中,行为未指定并且可能因实现而异。
将 iter() 应用于字典总是遍历键,但字典具有返回其他迭代器的方法。 如果要迭代值或键/值对,可以显式调用 values() 或 items() 方法来获得合适的迭代器。
dict() 构造函数可以接受一个迭代器,它返回一个有限的 (key, value)
元组流:
文件还通过调用 readline() 方法来支持迭代,直到文件中没有更多行。 这意味着您可以像这样读取文件的每一行:
集合可以从可迭代对象中获取它们的内容,并让您迭代集合的元素:
生成器表达式和列表推导式
迭代器输出的两个常见操作是 1) 对每个元素执行一些操作,2) 选择满足某些条件的元素子集。 例如,给定一个字符串列表,您可能希望从每一行中去除尾随空格或提取包含给定子字符串的所有字符串。
列表推导式和生成器表达式(简称:“listcomps”和“genexps”)是此类操作的简明符号,借用自函数式编程语言 Haskell (https://www.haskell.org/) . 您可以使用以下代码从字符串流中去除所有空格:
您可以通过添加 "if"
条件来仅选择某些元素:
通过列表理解,你会得到一个 Python 列表; stripped_list
是包含结果行的列表,而不是迭代器。 生成器表达式返回一个迭代器,它根据需要计算值,不需要一次实现所有值。 这意味着如果您正在使用返回无限流或大量数据的迭代器,则列表推导式没有用。 在这些情况下,生成器表达式更可取。
生成器表达式用括号(“()”)括起来,列表推导式用方括号(“[]”)括起来。 生成器表达式具有以下形式:
同样,对于列表理解,只有外括号不同(方括号而不是圆括号)。
生成的输出的元素将是 expression
的连续值。 if
子句都是可选的; 如果存在,仅当 condition
为真时才会评估 expression
并将其添加到结果中。
生成器表达式总是必须写在括号内,但发出函数调用信号的括号也算在内。 如果您想创建一个将立即传递给函数的迭代器,您可以编写:
for...in
子句包含要迭代的序列。 序列的长度不必相同,因为它们是从左到右迭代的, 不是 并行。 对于 sequence1
中的每个元素,sequence2
从头开始循环。 sequence3
然后为来自 sequence1
和 sequence2
的每个结果元素对循环。
换句话说,列表推导式或生成器表达式等效于以下 Python 代码:
这意味着当有多个 for...in
子句但没有 if
子句时,结果输出的长度将等于所有序列长度的乘积。 如果您有两个长度为 3 的列表,则输出列表的长度为 9 个元素:
为了避免在 Python 的语法中引入歧义,如果 expression
正在创建一个元组,它必须用括号括起来。 下面的第一个列表理解是一个语法错误,而第二个是正确的:
发电机
生成器是一类特殊的函数,可以简化编写迭代器的任务。 常规函数计算一个值并返回它,但生成器返回一个迭代器,它返回一个值流。
您无疑熟悉 Python 或 C 中常规函数调用的工作方式。 当您调用一个函数时,它会获得一个私有命名空间,在该命名空间中创建了它的局部变量。 当函数到达return
语句时,局部变量被销毁,并将值返回给调用者。 稍后对同一函数的调用会创建一个新的私有命名空间和一组新的局部变量。 但是,如果在退出函数时没有丢弃局部变量呢? 如果您稍后可以恢复它停止的功能怎么办? 这就是生成器提供的; 它们可以被认为是可恢复的功能。
这是生成器函数的最简单示例:
任何包含 yield 关键字的函数都是生成器函数; 这是由 Python 的 bytecode 编译器检测到的,该编译器因此专门编译该函数。
当您调用生成器函数时,它不会返回单个值; 相反,它返回一个支持迭代器协议的生成器对象。 在执行 yield
表达式时,生成器输出 i
的值,类似于 return
语句。 yield
和 return
语句之间的最大区别在于,在到达 yield
时,生成器的执行状态被挂起并保留局部变量。 在下次调用生成器的 __next__() 方法时,该函数将继续执行。
以下是 generate_ints()
生成器的示例用法:
你同样可以写 for i in generate_ints(5)
或 a, b, c = generate_ints(3)
。
在一个生成器函数中,return value
导致 StopIteration(value)
从 __next__() 方法被引发。 一旦发生这种情况,或者到达函数的底部,值的处理结束并且生成器不能产生任何进一步的值。
您可以通过编写自己的类并将生成器的所有局部变量存储为实例变量来手动实现生成器的效果。 例如,可以通过将 self.count
设置为 0 并让 __next__() 方法递增 self.count
并返回它来返回整数列表。 然而,对于一个中等复杂的生成器,编写相应的类可能会更加混乱。
Python 库中包含的测试套件 :source:`Lib/test/test_generators.py` 包含许多更有趣的示例。 这是一个使用生成器递归实现树的有序遍历的生成器。
test_generators.py
中的另外两个示例为 N-Queens 问题(将 N 个皇后放在 NxN 棋盘上,以便没有皇后威胁另一个皇后)和骑士之旅(找到一条将骑士带到每个方格的路线)的解决方案一个 NxN 棋盘,无需两次访问任何方块)。
将值传递给生成器
在 Python 2.4 及更早版本中,生成器只产生输出。 一旦调用生成器的代码来创建迭代器,在恢复执行时就无法将任何新信息传递给函数。 您可以通过让生成器查看全局变量或通过传入一些调用者然后修改的可变对象来组合这种能力,但这些方法是混乱的。
在 Python 2.5 中,有一种简单的方法可以将值传递给生成器。 yield 变成了一个表达式,返回一个可以赋值给变量或以其他方式操作的值:
我建议您 始终 在使用返回值执行某些操作时在 yield
表达式周围放置括号,如上例所示。 括号并不总是必要的,但总是添加它们更容易,而不必记住何时需要它们。
(PEP 342 解释了确切的规则,即 yield
表达式必须始终用括号括起来,除非它出现在右侧的顶级表达式中-作业的手边。 这意味着您可以编写 val = yield i
,但在进行操作时必须使用括号,如 val = (yield i) + 12
。)
通过调用其 send(value) 方法将值发送到生成器。 此方法恢复生成器的代码并且 yield
表达式返回指定的值。 如果调用常规的 __next__() 方法,则 yield
返回 None
。
这是一个简单的计数器,递增 1 并允许更改内部计数器的值。
这是更改计数器的示例:
因为 yield
经常会返回 None
,所以你应该经常检查这种情况。 不要只在表达式中使用它的值,除非您确定 send() 方法将是用于恢复生成器函数的唯一方法。
除了 send() 之外,生成器还有另外两种方法:
throw(type, value=None, traceback=None) 用于在生成器内部引发异常; 异常由
yield
表达式引发,其中生成器的执行暂停。close() 在生成器内部引发 GeneratorExit 异常以终止迭代。 收到此异常时,生成器的代码必须引发 GeneratorExit 或 StopIteration; 捕获异常并执行其他任何操作都是非法的,并且会触发 RuntimeError。 close() 在生成器被垃圾回收时也会被 Python 的垃圾回收器调用。
如果您需要在发生 GeneratorExit 时运行清理代码,我建议使用
try: ... finally:
套件而不是捕获 GeneratorExit。
这些变化的累积效应是将生成者从信息的单向生产者转变为生产者和消费者。
生成器也变成了 协程 ,一种更通用的子程序形式。 子程序在一个点进入并在另一个点退出(函数的顶部,和一个 return
语句),但是协程可以在许多不同的点进入、退出和恢复(yield
] 语句)。
内置功能
让我们更详细地了解经常与迭代器一起使用的内置函数。
Python 的两个内置函数 map() 和 filter() 复制了生成器表达式的特征:
- map(f, iterA, iterB, ...) 返回序列上的迭代器
f(iterA[0], iterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2]), ...
。
您当然可以使用列表理解来达到相同的效果。
filter(predicate, iter) 返回一个遍历所有满足特定条件的序列元素的迭代器,同样被列表推导式复制。 predicate 是一个返回某个条件真值的函数; 要与 filter() 一起使用,谓词必须采用单个值。
这也可以写成列表理解:
enumerate(iter, start=0) 对迭代中的元素进行计数,返回包含计数(来自 start)和每个元素的 2 元组。
enumerate() 在循环遍历列表并记录满足特定条件的索引时经常使用:
sorted(iterable, key=None, reverse=False) 将iterable的所有元素收集到一个列表中,对列表进行排序,返回排序后的结果。 key 和 reverse 参数被传递到构造列表的 sort() 方法。
(有关排序的更详细讨论,请参阅 Sorting HOW TO。)
any(iter) 和 all(iter) 内置函数查看可迭代内容的真值。 any() 如果迭代中的任何元素为真值,则返回 True
,如果所有元素都为真值,则 all() 返回 True
真实值:
zip(iterA, iterB, ...) 从每个可迭代对象中取出一个元素并在元组中返回它们:
它不会构造内存列表并在返回之前耗尽所有输入迭代器; 相反,元组仅在被请求时才被构造和返回。 (这种行为的技术术语是 懒惰评估 。)
此迭代器旨在与长度相同的可迭代对象一起使用。 如果可迭代对象的长度不同,则生成的流将与最短的可迭代对象长度相同。
但是,您应该避免这样做,因为元素可能会从较长的迭代器中取出并被丢弃。 这意味着您不能继续使用迭代器,因为您可能会跳过丢弃的元素。
itertools 模块
itertools 模块包含许多常用的迭代器以及用于组合多个迭代器的函数。 本节将通过展示小例子来介绍模块的内容。
该模块的功能分为几个大类:
- 基于现有迭代器创建新迭代器的函数。
- 将迭代器的元素视为函数参数的函数。
- 用于选择迭代器输出部分的函数。
- 用于对迭代器的输出进行分组的函数。
创建新的迭代器
itertools.count(start, step) 返回均匀间隔值的无限流。 您可以选择提供起始编号(默认为 0)和数字之间的间隔(默认为 1):
itertools.cycle(iter) 保存提供的可迭代内容的副本并返回一个新的迭代器,该迭代器从头到尾返回其元素。 新的迭代器将无限重复这些元素。
itertools.repeat(elem, [n]) 返回提供的元素 n 次,或者如果没有提供 n 则无限返回元素。
itertools.chain(iterA, iterB, ...) 将任意数量的迭代器作为输入,并返回第一个迭代器的所有元素,然后是第二个迭代器的所有元素,依此类推,直到所有可迭代对象都已用尽。
itertools.islice(iter, [start], stop, [step]) 返回一个流,它是迭代器的一个切片。 使用单个 stop 参数,它将返回第一个 stop 元素。 如果您提供起始索引,您将获得 stop-start 元素,如果您为 step 提供值,则将相应地跳过元素。 与 Python 的字符串和列表切片不同,您不能对 start、stop 或 step 使用负值。
itertools.tee(iter, [n]) 复制一个迭代器; 它返回 n 个独立迭代器,这些迭代器都将返回源迭代器的内容。 如果您不为 n 提供值,则默认值为 2。 复制迭代器需要保存源迭代器的一些内容,因此如果迭代器很大并且新迭代器之一比其他迭代器消耗更多,这会消耗大量内存。
在元素上调用函数
operator 模块包含一组对应于 Python 运算符的函数。 一些示例是 operator.add(a, b)(添加两个值)、operator.ne(a, b)(与 a != b
相同)和 [ X117X]operator.attrgetter('id')(返回一个获取 .id
属性的可调用对象)。
itertools.starmap(func, iter) 假设可迭代对象将返回一个元组流,并使用这些元组作为参数调用 func:
选择元素
另一组函数根据谓词选择迭代器元素的子集。
itertools.filterfalse(predicate, iter) 与 filter() 相反,返回谓词返回 false 的所有元素:
itertools.takewhile(predicate, iter) 返回元素,只要谓词返回 true。 一旦谓词返回 false,迭代器将发出结果结束的信号。
itertools.dropwhile(predicate, iter) 在谓词返回真时丢弃元素,然后返回可迭代结果的其余部分。
itertools.compress(data, selectors) 接受两个迭代器并只返回 data 中 selectors 的对应元素为真的那些元素,只要有一个就停止筋疲力尽:
组合函数
itertools.combinations(iterable, r) 返回一个迭代器,给出 iterable 中包含的元素的所有可能的 r 元组组合。
每个元组中的元素保持与 iterable 返回它们相同的顺序。 例如,在上述示例中,数字 1 始终位于 2、3、4 或 5 之前。 一个类似的函数,itertools.permutations(iterable, r=None),移除了这个对顺序的限制,返回所有可能的长度为 r 的排列:
如果您不为 r 提供值,则使用可迭代的长度,这意味着所有元素都被置换。
请注意,这些函数按位置生成所有可能的组合,并且不需要 iterable 的内容是唯一的:
相同的元组 ('a', 'a', 'b')
出现了两次,但两个 'a' 字符串来自不同的位置。
itertools.combinations_with_replacement(iterable, r) 函数放宽了不同的约束:元素可以在单个元组中重复。 从概念上讲,为每个元组的第一个位置选择一个元素,然后在选择第二个元素之前替换它。
分组元素
我将讨论的最后一个函数 itertools.groupby(iter, key_func=None) 是最复杂的。 key_func(elem)
是一个函数,可以为迭代返回的每个元素计算一个键值。 如果您不提供键函数,键就是每个元素本身。
groupby() 从底层迭代中收集所有具有相同键值的连续元素,并返回一个包含键值和具有该键的元素的迭代器的 2 元组流。
groupby() 假设底层迭代的内容已经根据键进行了排序。 请注意,返回的迭代器也使用底层迭代器,因此您必须在请求 iterator-2 及其对应的 key 之前使用 iterator-1 的结果。
功能工具模块
Python 2.5 中的 functools 模块包含一些高阶函数。 高阶函数 将一个或多个函数作为输入并返回一个新函数。 该模块中最有用的工具是 functools.partial() 函数。
对于以函数式风格编写的程序,您有时会希望构建填充了一些参数的现有函数的变体。 考虑一个 Python 函数 f(a, b, c)
; 你可能希望创建一个新的函数 g(b, c)
相当于 f(1, b, c)
; 您正在为 f()
的参数之一填充值。 这称为“部分功能应用”。
partial() 的构造函数采用参数 (function, arg1, arg2, ..., kwarg1=value1, kwarg2=value2)
。 结果对象是可调用的,因此您可以调用它来使用填充的参数调用 function
。
这是一个小而现实的例子:
functools.reduce(func, iter, [initial_value]) 累积对所有可迭代元素执行操作,因此不能应用于无限可迭代元素。 func 必须是一个接受两个元素并返回一个值的函数。 functools.reduce() 取迭代器返回的前两个元素 A 和 B 并计算 func(A, B)
。 然后它请求第三个元素 C,计算 func(func(A, B), C)
,将此结果与返回的第四个元素组合,并继续直到迭代用完。 如果可迭代对象根本没有返回值,则会引发 TypeError 异常。 如果提供了初始值,则将其用作起点,并且 func(initial_value, A)
是第一个计算。
如果您将 operator.add() 与 functools.reduce() 一起使用,您将把可迭代对象的所有元素相加。 这种情况非常常见,以至于有一个特殊的内置函数 sum() 来计算它:
但是,对于 functools.reduce() 的许多用途,只需编写明显的 for 循环会更清晰:
一个相关的函数是 itertools.accumulate(iterable, func=operator.add)。 它执行相同的计算,但不是只返回最终结果,accumulate()
返回一个迭代器,该迭代器也产生每个部分结果:
操作员模块
operator 模块前面已经提到过。 它包含一组对应于 Python 操作符的函数。 这些函数在函数式代码中通常很有用,因为它们使您无需编写执行单个操作的琐碎函数。
该模块中的一些功能是:
- 数学运算:
add()
、sub()
、mul()
、floordiv()
、abs()
、…… - 逻辑运算:
not_()
、truth()
。 - 按位运算:
and_()
、or_()
、invert()
。 - 比较:
eq()
、ne()
、lt()
、le()
、gt()
和ge()
。 - 对象标识:
is_()
、is_not()
。
有关完整列表,请参阅操作员模块的文档。
小函数和 lambda 表达式
在编写函数式程序时,您通常需要一些充当谓词或以某种方式组合元素的函数。
如果有合适的 Python 内置函数或模块函数,则根本不需要定义新函数:
如果您需要的函数不存在,则需要编写它。 编写小函数的一种方法是使用 lambda 表达式。 lambda
接受多个参数和组合这些参数的表达式,并创建一个返回表达式值的匿名函数:
另一种方法是使用 def
语句并以通常的方式定义函数:
哪个替代方案更可取? 这是一个风格问题; 我通常的做法是避免使用 lambda
。
我偏爱的原因之一是 lambda
在它可以定义的功能方面非常有限。 结果必须可以作为单个表达式进行计算,这意味着您不能进行多路 if... elif... else
比较或 try... except
语句。 如果您试图在 lambda
语句中做太多事情,最终会得到一个难以阅读的过于复杂的表达式。 快,下面的代码在做什么?
您可以弄清楚,但需要时间来理清表达式以弄清楚发生了什么。 使用简短的嵌套 def
语句会使事情变得更好:
但如果我只是使用 for
循环,那将是最好的:
或者 sum() 内置和生成器表达式:
functools.reduce() 的许多用途在编写为 for
循环时更加清晰。
Fredrik Lundh 曾经建议使用以下规则来重构 lambda
的使用:
- 编写一个 lambda 函数。
- 写一条评论来解释 lambda 的作用。
- 研究一下评论,然后想出一个能抓住评论本质的名字。
- 使用该名称将 lambda 转换为 def 语句。
- 删除评论。
我真的很喜欢这些规则,但你可以自由地不同意这种无 lambda 的风格是否更好。
修订历史和致谢
作者要感谢以下人员对本文的各种草稿提供建议、更正和帮助:Ian Bicking、Nick Coghlan、Nick Efford、Raymond Hettinger、Jim Jewett、Mike Krell、Leandro Lameiro、Jussi Salmela、Collin Winter,布莱克温顿。
0.1 版:2006 年 6 月 30 日发布。
0.11 版:2006 年 7 月 1 日发布。 错别字修复。
0.2 版:2006 年 7 月 10 日发布。 将 genexp 和 listcomp 部分合并为一个。 错别字修复。
0.21 版:在导师邮件列表中添加了更多建议参考。
0.30 版本:在 Collin Winter 编写的 functional
模块上添加了一个部分; 在操作员模块上添加了简短的部分; 其他一些编辑。
参考
一般
计算机程序的结构和解释 ,Harold Abelson 和 Gerald Jay Sussman 与 Julie Sussman 合着。 全文在 https://mitpress.mit.edu/sicp/。 在这本经典的计算机科学教科书中,第 2 章和第 3 章讨论了使用序列和流来组织程序内的数据流。 本书使用 Scheme 作为示例,但这些章节中描述的许多设计方法都适用于函数式 Python 代码。
http://www.defmacro.org/ramblings/fp.html:函数式编程的一般介绍,使用 Java 示例,并有冗长的历史介绍。
https://en.wikipedia.org/wiki/Functional_programming:描述函数式编程的一般维基百科条目。
https://en.wikipedia.org/wiki/Coroutine:协程入口。
https://en.wikipedia.org/wiki/Currying:柯里化概念的入口。
特定于 Python 的
http://gnosis.cx/TPiP/:David Mertz 的书 Text Processing in Python 的第一章讨论了文本处理的函数式编程,在标题为“利用高阶文本处理中的函数”。
Mertz 还为 IBM 的 DeveloperWorks 站点撰写了关于函数式编程的 3 部分系列文章; 见第1部分、第2部分和第3部分,