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

    生成器作用:节省内存空间。通过一边循环一边计算的机制称为生成器:generator

    创建生成器:

    1、将列表推导式的[]改为(),就创建了一个生成器。通过next()函数或for语句获取generator的值。

    >>> l = [x*x for x in range(10)]
    >>> l
    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    >>> g = (x*x for x in range(10))
    >>> g
    <generator object <genexpr> at 0x026F50A8>
    >>> next(g)
    0
    >>> next(g)
    1
    >>> next(g)
    4
    >>> next(g)
    9
    >>> next(g)
    16
    >>> next(g)
    25
    >>> next(g)
    36
    >>> next(g)
    49
    >>> next(g)
    64
    >>> next(g)
    81
    >>> next(g)
    Traceback (most recent call last):
      File "<pyshell#14>", line 1, in <module>
        next(g)
    StopIteration
    >>> g
    <generator object <genexpr> at 0x026F50A8>
    >>> for n in g:
        print(n)    
    >>> 
    >>> 
    >>> g = (x*x for x in range(10))
    >>> g
    <generator object <genexpr> at 0x026F8788>
    >>> for n in g:
        print(n)    
    0
    1
    4
    9
    16
    25
    36
    49
    64
    81
    >>

    2、通过函数的方式创建生成器:若一个函数中包含yield关键字,则此函数为一个generator

    def fib(max):
        n,a,b = 0,1,1
        while n < max:
            print(b)
            a, b = b, a+b
            n = n+1
        return "done"
    
    def fib1(max):
        n,a,b = 0,1,1
        while n < max:
            yield(b)
            a, b = b, a+b
            n = n+1
        return "done"
    >>> fib(6)
    1
    2
    3
    5
    8
    13
    'done'
    >>> fib1(6)
    <generator object fib1 at 0x03225210>
    >>> 
    >>> next(fib(6))
    1
    2
    3
    5
    8
    13
    Traceback (most recent call last):
      File "<pyshell#2>", line 1, in <module>
        next(fib(6))
    TypeError: 'str' object is not an iterator
    >>> next(fib1(6))
    1
    >>> for n in fib1(6)
    SyntaxError: invalid syntax
    >>> for n in fib1(6):
        print(n)
    1
    2
    3
    5
    8
    13
    >>> 
    def odd():
        print("step 1")
        yield 1
        print('step 2')
        yield 3
        print('step 3')
        yield 5
    >>> odd()
    <generator object odd at 0x02D051C0>
    >>> for i in odd():
        print(i)    
    step 1
    1
    step 2
    3
    step 3
    5
    >>> next(odd())
    step 1
    1
    >>> next(odd())
    step 1
    1
    >>> next(odd())
    step 1
    1
    >>> 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 "<pyshell#12>", line 1, in <module>
        next(o)
    StopIteration

    注意:使用for循环调用生成器时,不能获取生成器return语句的返回值。返回值包含在stopIteration的value

  • 相关阅读:
    jQuery中ajax的4种常用请求方式
    前端常见浏览器兼容性问题解决方案
    平衡二叉树---将有序数组转换为二叉搜索树
    equals
    Java中的System.out.println
    System.arraycopy --全面解释(就是那么有爱心)
    计算机网络实验-ACL-OSPF-RIP
    pip install lxml
    Spark-shell --常用命令 (command)
    《Thinking in Java》---Summary of Chapter 2
  • 原文地址:https://www.cnblogs.com/zhaoyujiao/p/5361245.html
Copyright © 2011-2022 走看看