zoukankan      html  css  js  c++  java
  • 生成器

    重要:yield的作用,类似于next(),执行一次yield,程序就准备输出下一行,会保存函数执行进度 (yield 相当于print,若yield迭代器要输出的话,一般用for循环输出)

    def odd():
        print('step 1')
        yield 1
        print('step 2')
        yield(3)
        print('step 3')
        yield(5)
    
    #============================================
    >>> o = odd()
    >>> next(o)
    step 1
    1
    >>> next(o)
    step 2
    3
    >>> next(o)
    step 3
    5
    >>> next(o)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    StopIteration

    通过列表生成式,我们可以直接创建一个列表。但是,受到内存限制,列表容量肯定是有限的。而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大多数元素占用的空间都白白浪费了。

    所以,如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的过程中不断推算出后续的元素呢?这样就不必创建完整的list,从而节省大量的空间。在Python中,这种一边循环一边计算的机制,称为生成器:generator。

    #生成杨辉三角
    # 期待输出:
    # [1]
    # [1, 1]
    # [1, 2, 1]
    # [1, 3, 3, 1]
    # [1, 4, 6, 4, 1]
    # [1, 5, 10, 10, 5, 1]
    # [1, 6, 15, 20, 15, 6, 1]
    # [1, 7, 21, 35, 35, 21, 7, 1]
    # [1, 8, 28, 56, 70, 56, 28, 8, 1]
    # [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

    方法一:

    def triangles():
        listt = [1]
        while True:
            yield listt
            listt.append(0)
            listt = [listt[i-1]+listt[i] for i in range(len(listt))]
    
    t = triangles()
    for i in range(10):
        print(next(t))
    
    #==========================================
    [1]
    [1, 1]
    [1, 2, 1]
    [1, 3, 3, 1]
    [1, 4, 6, 4, 1]
    [1, 5, 10, 10, 5, 1]
    [1, 6, 15, 20, 15, 6, 1]
    [1, 7, 21, 35, 35, 21, 7, 1]
    [1, 8, 28, 56, 70, 56, 28, 8, 1]
    [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

    方法二:

    def triangles():
        listt = [1]
        while True:
            yield listt
            listt = [sum(i) for i in zip([0]+listt,listt+[0])]
      
    t = triangles()
    for i in range(10):
        print(next(t))
    #===========================================
    [1]
    [1, 1]
    [1, 2, 1]
    [1, 3, 3, 1]
    [1, 4, 6, 4, 1]
    [1, 5, 10, 10, 5, 1]
    [1, 6, 15, 20, 15, 6, 1]
    [1, 7, 21, 35, 35, 21, 7, 1]
    [1, 8, 28, 56, 70, 56, 28, 8, 1]
    [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

    dd

  • 相关阅读:
    salt+jenkins+gitlab+ecs构建公司部署平台
    解决openstack实例主机名后缀问题
    测试CentOS-7-x86_64-Minimal-1708.iso这种光盘安装效果
    dbca建库--linux上使用vnc图形化安装oracle10g版本
    使用mediainfo工具统计每个视频文件(媒体文件)播放时长
    rsync命令简单用法介绍
    winscp工具和xshell连接linux机器时切换到root账户
    zabbix和iptables的nat表结合使用
    adb和机顶盒一些常识
    VirtualBox虚拟机禁止时间同步
  • 原文地址:https://www.cnblogs.com/hanggegege/p/5796036.html
Copyright © 2011-2022 走看看