zoukankan      html  css  js  c++  java
  • Python3.x基础学习-生成器用法

    生成器

    生成器的本质就是迭代器
    生成器包括两种:生成器函数和生成器表达式

    一个包含yield关键字的函数就是一个生成器函数。并且yield不能和return共用,并且yield只能用在函数内
    (1)生成器函数执行之后会得到一个生成器作为返回值,并不会执行函数体。
    (2)执行了__next__()方法之后才会执行函数体,并且获得返回值
    (3) next()内置方法,内部调用生成器函数的__next__()方法。
    (4)yield和return相同的是可以返回值,但是不同的是yield不会结束函数

    def func():
        print('---func---')
        yield 1
    
    g = func()
    # print(g) 
    print(g.__next__())
    print(next(g))   # StopIteration
    
    # ---func---
    # 1
    def func():
        print('---func---')
        yield 1
        print('---func2---')
        yield 2
    g= func()
    print(g.__next__())
    # print(g.__next__())
    # print(g.__next__())  #StopIteration
    print(next(g))
    
    # ---func---
    # 1
    # ---func2---
    # 2
    def fib(n):
        index = 0
        a,b =0,1
        print('---1---')
        while index<n:
            print('---2---')
            a,b = b,a+b
            yield a
            index+=1
            print('----3---')
    
    g = fib(5)
    print(next(g))
    print(next(g))
    print(next(g))
    print(next(g))
    print(next(g))
    
    # ---1---
    # ---2---
    # 1
    # ----3---
    # ---2---
    # 1
    # ----3---
    # ---2---
    # 2
    # ----3---
    # ---2---
    # 3
    # ----3---
    # ---2---
    # 5
    import time
    
    def ksf():
        for i in range(100000):
            yield '
    {}桶方便面'.format(i)
            time.sleep(0.01)
    
    
    g = ksf()
    for i in range(100000):
        print(next(g))
    
    # 0桶方便面
    # 1桶方便面
    # 2桶方便面
    #...
    # 100000桶方便面

    send()

    send 获取下一个值的效果和next()基本一直,只是在获取下一个值的时候,给上一个yeild的位置传递一个数据
    使用send的注意事项
    (1)第一次使用生成器的时候是用next获取下一个值,不能第一次使用send,或者使用g.send(None)唤醒
    (2)最后一个yield不能接手外部的值

    def generator():
        print('a')
        count = yield 1
        print('--->',count)
        print('b')
        yield 2
        print('c')
    g = generator()
    print(next(g))
    print(g.send(None))
    
    # a
    # 1
    # ---> None
    # b
    # 2
    
    
    # 求移动平均值
    #10 10
    #20 15
    #30 20

    def
    func(): sum = 0 count = 0 avg = 0 while True: print('---1----') get_num =yield avg print('---2----') sum +=get_num count+=1 print('---3----') avg = sum/count g = func() next(g) print(g.send(10)) print(g.send(20)) print(g.send(30)) # ---1---- # ---2---- # ---3---- # ---1---- # 10.0 # ---2---- # ---3---- # ---1---- # 15.0 # ---2---- # ---3---- # ---1---- # 20.0
    # yield from循环遍历容器类型  返回一个新的迭代器
    
    def func():
        for i in 'hello':
            yield i
        for j in range(3):
            yield j
    f = func()
    print(next(f))
    print(next(f))
    print(next(f))
    print(next(f))
    print(next(f))
    print(next(f))
    print(next(f))
    
    # h
    # e
    # l
    # l
    # o
    # 0
    # 1

    # 同样

    def func():
    yield from 'hello'
    yield from range(3)

    f = func()
    for i in f:
    print(i)
     
    # #生成器表达式
    # # 格式:将列表解析式[]改成()即可
    lst=[i for i in range(10)]
    print(lst)
    iter1 = iter(lst)
    print(next(iter1))
    
    g = (i for i in range(10))
    print(g)
    print(next(g),'----0----')
    print(next(g),'----1----')
    
    for i in g:
        print(i)
    
    # 0 ----0----
    # 1 ----1----
    # 2
    # 3
    # 4
    # 5
    # 6
    # 7
    # 8
    # 9
  • 相关阅读:
    新建安卓项目后,manifest.xml中会出现大段的黄色警告
    TextView设置setCompoundDrawables不生效解决办法
    数据结构->队列->顺序循环队列ADT代码
    数据结构->栈->顺序栈ADT代码
    JAVA环境变量的配置
    计算机科学的范围-----18.12.08
    字符串复制
    扔鸡蛋问题和找零钱问题
    动态规划
    最小二乘法
  • 原文地址:https://www.cnblogs.com/johnsonbug/p/12709693.html
Copyright © 2011-2022 走看看