python中生成器_Python中的生成器

python中生成器

Generators are similar to list comprehensions but are surrounded by parentheses. The generators and list comprehension produce the same result but operate differently.

生成器类似于列表推导,但用括号括起来。 生成器和列表理解产生相同的结果,但操作方式不同。

When a list comprehension executes, it produces all of its data before any other processing occurring. The list comprehension takes a long time to produce data, delays any other code from running until the list comprehension concludes.

当执行列表理解时,它会在进行任何其他处理之前生成所有数据。 列表理解需要很长时间才能生成数据,将其他任何代码的运行延迟到列表理解结束为止。

For example,

例如,

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> for i in [x*3 for x in [1,2,3,4]]:
...     print(i)
...
3
6
9
12
>>>

In the above example, since the list is small the memory consumed is very less. However, if the list produces an enormous number of items then we have an issue.

在上面的示例中,由于列表很小,因此消耗的内存非常少。 但是,如果列表中产生了大量项目,那么我们就会遇到问题。

生成器一次生成一个数据项 (Generators produce data items one at a time)

The above list comprehension is modified to be a generator as below,

上面的列表理解被修改为如下生成器,

>>> for i in (x*3 for x in [1,2,3,4]):
...     print(i)
...

Unlike in the list comprehension, which must wrap up before any other code can execute, a generator yields data as soon as the data is produced by the generators code, which means if the generator generates enormous data items, and any code that’s waiting to consume the result generated executes immediately.

与列表解析不同,列表解析必须在任何其他代码可以执行之前进行包装,而生成器会在生成器代码生成数据后立即生成数据,这意味着生成器是否生成大量数据项以及任何等待使用的代码生成的结果立即执行。

Example:

例:

Using list comprehension, in the below example the response for both the URLs is returned at same time, which means the print statement waits for the list to perform its action,

在以下示例中, 使用list comprehension可以同时返回两个URL的响应,这意味着print语句等待列表执行其操作,

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import requests
>>> from datetime import datetime
>>> urls = ('http://www.includehelp.com', 'https://www.linkedin.com/')
>>> for resp in [requests.get(url) for url in urls]:
...     print(len(resp.content), '->', resp.status_code, '->', resp.url, '->', datetime.now())
...
132281 -> 200 -> https://www.includehelp.com/ -> 2019-10-30 17:51:41.099558
82131 -> 200 -> https://www.linkedin.com/ -> 2019-10-30 17:51:41.099726

Using generators, in the below example the response time for each of URL in the URLs is considerable different and explains the generator execution.

使用生成器 ,在下面的示例中,URL中每个URL的响应时间都大不相同,并说明了生成器的执行。

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import requests
>>> from datetime import datetime
>>> urls = ('http://www.includehelp.com', 'https://www.linkedin.com/')
>>> for resp in (requests.get(url) for url in urls):
...     print(len(resp.content), '->', resp.status_code, '->', resp.url, '->', datetime.now())
...
132281 -> 200 -> https://www.includehelp.com/ -> 2019-10-30 17:51:54.832263
82124 -> 200 -> https://www.linkedin.com/ -> 2019-10-30 17:51:54.971670


翻译自: https://www.includehelp.com/python/generators.aspx

python中生成器

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值