美文网首页
迭代器和生成器

迭代器和生成器

作者: 大冰S | 来源:发表于2019-12-02 12:03 被阅读0次

    迭代器和生成器

    迭代器

    迭代
    可迭代对象

    hasattr(list,'iter')
    True

    判断列表是否为可迭代对象


    迭代器

    lst = [1,2,3]
    hasattr(lst,'next')
    False ——列表不是迭代器
    iter_lst = iter(lst) ——创建迭代器
    iter_lst
    <list_iterator object at 0x00000123E17DE820>
    iter_lst.next()
    1
    iter_lst.next()
    2
    iter_lst.next()
    3
    iter_lst.next()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    StopIteration

    迭代器执行过程
    for循环

    import dis
    dis.dis('for i in lst:pass')
    1 0 LOAD_NAME 0 (lst)
    2 GET_ITER
    4 FOR_ITER 4 (to 10)
    6 STORE_NAME 1 (i)
    8 JUMP_ABSOLUTE 4
    10 LOAD_CONST 0 (None)
    12 RETURN_VALUE

    另一种生成迭代器对象的方式

    import itertools
    c = itertools.count(start = 3)
    dir(c)
    ['class', 'delattr', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'gt', 'hash', 'init', 'init_subclass', 'iter', 'le', 'lt', 'ne', 'new', 'next', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof', 'str', 'subclasshook']
    next(c)
    3
    next(c)
    4
    colors = itertools.cycle(['red', 'green', 'blue']) —— 可循环迭代器对象
    next(colors)
    'red'
    next(colors)
    'green'
    next(colors)
    'blue'
    next(colors)
    'red'

    生成器

    • 类似普通函数,不同点在于其包含yield表达式
    • 生成器也是迭代器

    def g():
    ... yield 0
    ... yield 1
    ... yield 2
    ...
    ge = g()
    dir(ge)
    ['class', 'del', 'delattr', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'gt', 'hash', 'init', 'init_subclass', 'iter', 'le', 'lt', 'name', 'ne', 'new', 'next', 'qualname', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof', 'str', 'subclasshook', 'close', 'gi_code', 'gi_frame', 'gi_running', 'gi_yieldfrom', 'send', 'throw']
    ge
    <generator object g at 0x00000123E34B7F20>
    ge.next()
    0
    ge.next()
    1
    ge.next()
    2
    ge.next()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    StopIteration

    def y_yield(n): ————定义生成器对象函数
    ... while n>0:
    ... print('before yield')
    ... yield n
    ... n -= 1
    ... print('after yield')
    ...
    yy = y_yield(3)
    yy.next()
    before yield
    3
    yy.next()
    after yield
    before yield
    2
    yy.next()
    after yield
    before yield
    1
    yy.next()
    after yield
    next(c)
    3
    >>> next(c)
    4
    >>> colors = itertools.cycle(['red', 'green', 'blue']) —— 可循环迭代器对象
    >>> next(colors)
    'red'
    >>> next(colors)
    'green'
    >>> next(colors)
    'blue'
    >>> next(colors)
    'red'

    ##生成器
    - 类似普通函数,不同点在于其包含yield表达式
    - 生成器也是迭代器

    >>> def g():
    ... yield 0
    ... yield 1
    ... yield 2
    ...
    >>> ge = g()
    >>> dir(ge)
    ['class', 'del', 'delattr', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'gt', 'hash', 'init', 'init_subclass', 'iter', 'le', 'lt', 'name', 'ne', 'new', 'next', 'qualname', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof', 'str', 'subclasshook', 'close', 'gi_code', 'gi_frame', 'gi_running', 'gi_yieldfrom', 'send', 'throw']
    >>> ge
    <generator object g at 0x00000123E34B7F20>
    >>> ge.next()
    0
    >>> ge.next()
    1
    >>> ge.next()
    2
    >>> ge.next()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    StopIteration

    >>> def y_yield(n): ————定义生成器对象函数
    ... while n>0:
    ... print('before yield')
    ... yield n
    ... n -= 1
    ... print('after yield')
    ...
    >>> yy = y_yield(3)
    >>> yy.next()
    before yield
    3
    >>> yy.next()
    after yield
    before yield
    2
    >>> yy.next()
    after yield
    before yield
    1
    >>> yy.next()
    after yield
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    StopIteration

    生成器解析

    相关文章

      网友评论

          本文标题:迭代器和生成器

          本文链接:https://www.haomeiwen.com/subject/tvjxgctx.html