侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Python进阶:生成器 懒人版本的迭代器详解

Python  /  管理员 发布于 7年前   177

从容器、可迭代对象谈起

所有的容器都是可迭代的(iterable),迭代器提供了一个next方法。iter()返回一个迭代器,通过next()函数可以实现遍历。

def is_iterable(param):try: iter(param) return Trueexcept TypeError:return Falseparams = [1234,'1234',[1, 2, 3, 4],set([1, 2, 3, 4]),{1:1, 2:2, 3:3, 4:4},(1, 2, 3, 4)]for param in params:print('{} is iterable? {}'.format(param, is_iterable(param)))########## 输出 ########### 1234 is iterable? False# 1234 is iterable? True# [1, 2, 3, 4] is iterable? True# {1, 2, 3, 4} is iterable? True# {1: 1, 2: 2, 3: 3, 4: 4} is iterable? True# (1, 2, 3, 4) is iterable? True

除了数字外,其他数据结构都是可迭代的。

生成器是什么

生成器是懒人版本的迭代器。例:

import osimport psutil#显示当前 python 程序占用的内存大小def show_memory_info(hint):pid = os.getpid()p = psutil.Process(pid)info = p.memory_full_info()memory = info.uss / 1024. / 1024print('{} memory used: {} MB'.format(hint, memory))def test_iterator():show_memory_info('initing iterator')list_1 = [i for i in range(100000000)]show_memory_info('after iterator initiated')print(sum(list_1))show_memory_info('after sum called')def test_generator():show_memory_info('initing generator')list_2 = (i for i in range(100000000))show_memory_info('after generator initiated')print(sum(list_2))show_memory_info('after sum called')test_iterator()test_generator()%time test_iterator()%time test_generator()######### 输出 ##########initing iterator memory used: 48.9765625 MBafter iterator initiated memory used: 3920.30078125 MB4999999950000000after sum called memory used: 3920.3046875 MBWall time: 17 siniting generator memory used: 50.359375 MBafter generator initiated memory used: 50.359375 MB4999999950000000after sum called memory used: 50.109375 MBWall time: 12.5 s

[i for i in range(100000000)] 声明了一个迭代器,每个元素在生成后都会保存到内存中,占用了巨量的内存。(i for i in range(100000000)) 初始化了一个生成器,可以看到,生成器并不会像迭代器一样占用大量的内存,相比于 test_iterator(),test_generator()函数节省了一次生成一亿个元素的过程。在调用next()的时候,才会生成下一个变量.

生成器能玩啥花样

数学中有一个恒等式,(1 + 2 + 3 + ... + n)^2 = 1^3 + 2^3 + 3^3 + ... + n^3,用以下代码表达

def generator(k):i = 1while True:yield i ** ki += 1gen_1 = generator(1)gen_3 = generator(3)print(gen_1)print(gen_3)def get_sum(n):sum_1, sum_3 = 0, 0for i in range(n):next_1 = next(gen_1)next_3 = next(gen_3)print('next_1 = {}, next_3 = {}'.format(next_1, next_3))sum_1 += next_1sum_3 += next_3print(sum_1 * sum_1, sum_3)get_sum(8)########## 输出 ########### <generator object generator at 0x000001E70651C4F8># <generator object generator at 0x000001E70651C390># next_1 = 1, next_3 = 1# next_1 = 2, next_3 = 8# next_1 = 3, next_3 = 27# next_1 = 4, next_3 = 64# next_1 = 5, next_3 = 125# next_1 = 6, next_3 = 216# next_1 = 7, next_3 = 343# next_1 = 8, next_3 = 512# 1296 1296

generator()这个函数,它返回了一个生成器,当运行到yield i ** k时,暂停并把i ** k作为next()的返回值。每次调用next(gen)时,暂停的程序会启动并往下执行,而且i的值也会被记住,继续累加,最后next_1为8,next_3为512.

仔细查看这个示例,发现迭代器是一个有限集合,生成器则可以成为一个无限集。调用next(),生成器根据运算会自动生成新的元素,然后返回给你,非常便捷。

再来看一个问题:给定一个list和一个指定数字,求这个数字在list中的位置:

#常规写法def index_normal(L, target):result = []for i, num in enumerate(L):if num == target:result.append(i)return resultprint(index_normal([1, 6, 2, 4, 5, 2, 8, 6, 3, 2], 2))########## 输出 ##########[2, 5, 9]#生成器写法def index_generator(L, target):for i, num in enumerate(L):if num == target:yield iprint(list(index_generator([1, 6, 2, 4, 5, 2, 8, 6, 3, 2], 2)))######### 输出 ##########[2, 5, 9]

再看一例子:

查找子序列:给定两个字符串a,b,查找字符串a是否字符串b的子序列,所谓子序列,即一个序列包含在另一个序列中并且顺序一

算法:分别用两个指针指向两个字符串的头,然后往后移动找出相同的值,如果其中一个指针走完了整个字符串也没有相同的值,则不是子序列

def is_subsequence(a, b):b = iter(b)return all(i in b for i in a)print(is_subsequence([1, 3, 5], [1, 2, 3, 4, 5]))print(is_subsequence([1, 4, 3], [1, 2, 3, 4, 5]))######### 输出 ##########TrueFalse

下面代码为上面代码的演化版本

def is_subsequence(a, b):b = iter(b)print(b)gen = (i for i in a)print(gen)for i in gen:print(i)gen = ((i in b) for i in a)print(gen)for i in gen:print(i)return all(((i in b) for i in a))print(is_subsequence([1, 3, 5], [1, 2, 3, 4, 5]))print(is_subsequence([1, 4, 3], [1, 2, 3, 4, 5]))########## 输出 ########### <list_iterator object at 0x000001E7063D0E80># <generator object is_subsequence.<locals>.<genexpr> at 0x000001E70651C570># 1# 3# 5# <generator object is_subsequence.<locals>.<genexpr> at 0x000001E70651C5E8># True# True# True# False# <list_iterator object at 0x000001E7063D0D30># <generator object is_subsequence.<locals>.<genexpr> at 0x000001E70651C5E8># 1# 4# 3# <generator object is_subsequence.<locals>.<genexpr> at 0x000001E70651C570># True# True# False# False

首先iter(b)把b转为迭代器。目的是内部实现next函数,(i for i in a) 会产生一个生成器 ,同样((i in b) for i in a)也是。然后(i in b)等阶于:

while True:val = next(b)if val == i:yield True

这里非常巧妙地利用生成器的特性,next()函数运行的时候,保存了当前的指针。比如下面这个示例

b = (i for i in range(5))print(2 in b)print(4 in b)print(3 in b)########## 输出 ##########TrueTrueFalse

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    python 基于TCP协议的套接字编程详解
    下一条:
    简单了解Python matplotlib线的属性
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客