Python生成器教程

Python generators are a powerful, but misunderstood tool. They’re often treated as too difficult a concept for beginning programmers to learn — creating the illusion that beginners should hold off on learning generators until they are ready. I think this assessment is unfair, and that you can use generators sooner than you think.

Python生成器是一个功能强大但被误解的工具。 对于初学者来说,他们常常被认为是一个过于困难的概念,从而产生了一种幻象,即初学者应该在准备好生成器之前坚持学习。 我认为这种评估是不公平的,您可以比您想象的更早使用发电机。

In this tutorial, we’ll cover:

在本教程中,我们将介绍:

  • The basic terminology needed to understand generators
  • What a generator is
  • How to create your own generators
  • How to use a generator and generator methods
  • When to use a generator
  • 了解生成器所需的基本术语
  • 什么是发电机
  • 如何创建自己的发电机
  • 如何使用生成器和生成器方法
  • 何时使用发电机

先决条件 (Prereqs)

To get the most out of this tutorial, you should be familiar with the following concepts:

为了充分利用本教程,您应该熟悉以下概念:

  • Basic Python data structures
    • What a list is
    • What a dictionary is
  • Functions
    • What a function is
    • How to create and use functions
  • List Comprehensions
    • What a list comprehension is
    • How to create a simple list comprehension
  • 基本的Python数据结构
    • 什么是清单
    • 什么是字典
  • 功能
    • 什么是功能
    • 如何创建和使用功能
  • 清单理解
    • 什么是列表理解
    • 如何创建一个简单的列表理解

基本术语 (Basic terms to know)

迭代和可迭代 (Iteration and iterables)

Iteration is the repetition of some kind of process over and over again. Python’s for loop gives us an easy way to iterate over various objects. Often, you’ll iterate over a list, but we can also iterate over other Python objects such as strings and dictionaries.

迭代是一遍又一遍地重复某种过程。 Python的for循环为我们提供了一种迭代各种对象的简便方法。 通常,您将遍历一个列表,但是我们也可以遍历其他Python对象,例如字符串和字典。

# Iterating over a list
ez_list = [1, 2, 3]
for i in ez_list:
    print(i)
>>> 1
>>> 2
>>> 3

# Iterating over a string
ez_string = "Generators"
for s in ez_string:
    print(s)
>>> "G"
>>> "e"
...
>>> "r"
>>> "s"

# Iterating over a dictionary
ez_dict = {1 : "First, 2 : "Second"}
for key, value in ez_dict.items():
    print(k, v)
>>> 1 "First"
>>> 2 "Second"
# Iterating over a list
ez_list = [1, 2, 3]
for i in ez_list:
    print(i)
>>> 1
>>> 2
>>> 3

# Iterating over a string
ez_string = "Generators"
for s in ez_string:
    print(s)
>>> "G"
>>> "e"
...
>>> "r"
>>> "s"

# Iterating over a dictionary
ez_dict = {1 : "First, 2 : "Second"}
for key, value in ez_dict.items():
    print(k, v)
>>> 1 "First"
>>> 2 "Second"
 

In each of the above examples, the for loop iterates over the sequence we give it. The code above used a list, string, and dictionary, but you can iterate over tuples and sets as well. In each loop above, we print each of the items in the sequence in the order they appear. For example, you can confirm that the order of the ez_list is replicated in the order that its items are printed out.

在上述每个示例中, for循环遍历我们给出的序列。 上面的代码使用了列表,字符串和字典,但是您也可以遍历元组和集合。 在上面的每个循环中,我们按照出现的顺序print序列中的每个项目。 例如,您可以确认ez_list的顺序已按照其项目的打印顺序进行复制。

对于循环解剖

We refer to any object that can support iteration as an iterable.

我们将可以支持迭代的任何对象称为可迭代对象

什么定义了迭代? (What defines an iterable?)

Iterables support something called the Iterator Protocol. The technical definition for the Iterator Protocol is out of the scope of this article, but it can be thought of as a set of requirements to be used for a for loop. That is to say: lists, strings and dictionaries all follow the Iterator Protocol, therefore we can use them in for loops. Conversely, objects that do not follow the protocol cannot be used in a for loop. One example of an object that does not follow the protocol is an integer.

迭代支持所谓的迭代器协议 。 Iterator协议的技术定义不在本文的讨论范围之内,但是可以将其视为用于for循环的一组要求。 也就是说:列表,字符串和字典都遵循Iterator协议,因此我们可以在for循环中使用它们。 相反,不遵循该协议的对象不能在for循环中使用。 不遵循该协议的对象的一个​​示例是整数。

If we try to give an integer to a for loop, Python will throw an error.

如果我们尝试为for循环提供一个整数,Python将抛出错误。

An integer is just a singular number, not a sequence. You may argue that the “first” number in number is 1, but it is not the same as the first item in a sequence. It doesn’t make sense to ask “What’s after 1?” from number since Python only understands integers as a single entities.

整数只是一个单数,而不是一个序列。 您可能会认为,在“第一”数number为1,但它是不一样的序列中的第一项。 问“ 1之后是什么?”是没有意义的。 从number开始,因为Python仅将整数理解为单个实体。

Therefore, one of the requirements to be an iterable is to be able to describe to the for loop what the next item to perform the operation on is. For example, lists tell the for loop that the next item to iterate on is in the index+1 from the current one (1 comes after 0).

因此,可迭代的要求之一是能够向for循环描述执行操作的下一项是什么。 例如,列表告诉for循环,下一个要迭代的项位于当前项的index + 1中(0后面是1)。

Consequently, an iterable must also signal to a for loop when to stop iterating. This signal usually comes when we arrive at the end of a sequence (i.e. the end of a list or string). We will explore the specific functions that make something iterable later in this article, the important thing to know is that iterables describe how a for loop should traverse its contents.

因此,一个可迭代对象还必须在停止迭代时向for循环发出信号。 当我们到达序列的末尾(即列表或字符串的末尾)时,通常会出现此信号。 我们将在本文后面探讨使某些事物可迭代的特定函数,要知道的重要一点是,可迭代对象描述了for循环应如何遍历其内容。

Generators are iterables themselves. As you’ll see later, for loops are one of the main ways we use a generator, so they must be able to support iteration. We’ll delve into how we can create our own generators in the next secton.

生成器本身就是可迭代的。 正如您将在后面看到的, for循环是我们使用生成器的主要方式之一,因此它们必须能够支持迭代。 我们将深入研究如何在下一秒创建自己的生成器。

关键要点:要了解的基本术语 (Key takeaways: basic terms to know)

  • Iteration is the idea of repeating some process over a sequence of items. In Python, iteration is usually related to the for loop.

  • An iterable is an object that supports iteration.

  • To be an iterable, it must describe to a for loop two things:

    1. What item comes next in the iteration.
    2. When should the loop stop iteration.
  • Generators are iterables.

  • 迭代是在一系列项目上重复某些过程的想法。 在Python中,迭代通常与for循环相关。

  • 可迭代对象是支持迭代的对象。

  • 要进行迭代,它必须向for循环描述两件事:

    1. 迭代中的下一个项目是什么。
    2. 循环何时应停止迭代。
  • 生成器是可迭代的。

基于数据的方法 (A data-based approach)

To truly explore generators, we’ll use the Brewer’s Friend Beer Recipes data set from Kaggle. You can find the data set here, if you’d like to follow along on your own computer.

为了真正探索发电机,我们将使用Kaggle提供的Brewer's Friend Beer Recipes数据集。 如果您想在自己的计算机上继续学习,可以在这里找到数据集。

The data contains important beer characteristics from brewers around the world, including style of beer, alcohol by volume (ABV), and amount of beer produced. For the purposes of this article, let’s say that we are interested in brewing our own beer. Perhaps we want to sell our beer, so we would like to see what others have done to inform our brewing choices and produce more popular beer styles.

数据包含来自世界各地啤酒商的重要啤酒特性,包括啤酒样式,酒精含量(ABV)和啤酒产量。 就本文而言,假设我们对酿造自己的啤酒感兴趣。 也许我们想出售我们的啤酒,所以我们想看看其他人做了些什么,以告知我们的酿造选择并生产更多流行的啤酒样式。

Author’s Note: The “Name” column in the original data set contains some messy values that interfere with our analysis. You can find a cleaned version that will serve our purposes here.

作者注:原始数据集中的“名称”列包含一些凌乱的值,这些值会干扰我们的分析。 您可以在此处找到可以满足我们目的的清理版本。

发电机和你

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值