Skip to Content
《学习 Python》第 5 版
book

《学习 Python》第 5 版

by Mark Lutz
May 2025
Intermediate to advanced
1648 pages
23h 5m
Chinese
O'Reilly Media, Inc.
Content preview from 《学习 Python》第 5 版

第 14 章 迭代和理解 迭代和综合

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在上一章中,我们认识了 Python 的两个循环语句whilefor 。虽然它们可以处理程序需要执行的大多数重复任务,但对序列进行遍历的需求是如此常见和普遍,以至于 Python 提供了额外的工具来使其更简单、更高效。本章将开始我们对这些工具的探索。具体地说,它介绍了 Python 的迭代协议的相关概念、for 循环使用的方法调用模型,以及列表理解的一些细节,列表理解for循环的近亲,它将表达式应用到可迭代的项目中。

由于这些工具与for 循环和函数都有关系,因此我们将在本书中分两部分介绍它们,并附有后记:

  • 本章将在循环工具的背景下介绍它们的基础知识,作为前一章的延续。

  • 第 20 章将在基于函数的工具中重新讨论它们,并将主题扩展到内置生成器和用户定义生成器

  • 第 30 章也是本故事的最后一个短篇,我们将学习用类编码的用户定义可迭代对象。

在本章中,我们还将举例说明 Python 中的其他迭代工具,并介绍 Python 3.X 中的新迭代表--在 Python 3.X 中,迭代表的概念变得更加普遍。

首先要说明的是:这几章中介绍的一些概念乍看起来可能比较高深。但通过实践,您会发现这些工具非常有用且功能强大。尽管由于这些工具在 Python 代码中已经司空见惯,所以从来没有严格的要求,但如果您必须阅读他人编写的程序,对这些工具的基本理解也会有所帮助。

迭代:初体验

在上一章 中,我提到for循环可以作用于 Python 中的任何序列类型,包括列表、元组和字符串,就像这样:

>>> for x in [1, 2, 3, 4]: print(x ** 2, end=' ')            # In 2.X: print x ** 2,
...
1 4 9 16

>>> for x in (1, 2, 3, 4): print(x ** 3, end=' ')
...
1 8 27 64

>>> for x in 'spam': print(x * 2, end=' ')
...
ss pp aa mm

实际上,for 循环比这还要通用--它可以在任何可迭代对象上运行。事实上,Python 中所有从左到右扫描对象的迭代工具都是如此,包括for 循环、我们将在本章学习的 list 解析、in 成员测试、map 内置函数等等。

在 Python 中,"可迭代对象 "的概念相对较新,但它已渗透到语言的设计中。它本质上是对序列概念的概括--如果一个对象是一个物理存储的序列,或者是一个在for 循环等迭代工具中一次产生一个结果的对象,那么这个对象就被认为是可迭代的。从某种意义上说,可迭代对象既包括物理序列,也包括按需计算的虚拟序列

备注

本主题中的术语往往有点松散。可迭代 "和 "迭代器 "这两个术语有时可以互换使用,泛指支持迭代的对象。为清晰起见,本书更倾向于使用iterable来指代支持iter 调用的对象,而iterator则指由支持iter 调用的 iterable 返回的对象。 next(I) 调用返回的对象。这两种调用都在前面进行了定义。

但这一约定在 Python 世界和本书中并不通用;"迭代器 "有时也用于迭代工具。第 20 章用术语 "生成器 "扩展了这一类别--它指自动支持迭代协议的对象,因此是可迭代的--尽管所有可迭代的对象都会生成结果!

迭代协议文件迭代器

要理解迭代协议,最简单的方法之一就是看看它是如何与文件等内置类型一起工作的。在本章中,我们将使用以下输入文件进行演示: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

学习 Java,第 6 版

学习 Java,第 6 版

Marc Loy, Patrick Niemeyer, Daniel Leuck
《高性能 Python》第二版

《高性能 Python》第二版

Micha Gorelick, Ian Ozsvald

Publisher Resources

ISBN: 9798341656963