迭代器和生成器

迭代器和生成器

迭代器

迭代器和生成器_第1张图片
迭代

迭代器和生成器_第2张图片
可迭代对象

hasattr(list,'iter')
True

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


迭代器和生成器_第3张图片
迭代器

lst = [1,2,3]
hasattr(lst,'next')
False ——列表不是迭代器
iter_lst = iter(lst) ——创建迭代器
iter_lst

iter_lst.next()
1
iter_lst.next()
2
iter_lst.next()
3
iter_lst.next()
Traceback (most recent call last):
File "", line 1, in
StopIteration

迭代器和生成器_第4张图片
迭代器执行过程

迭代器和生成器_第5张图片
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

ge.next()
0
ge.next()
1
ge.next()
2
ge.next()
Traceback (most recent call last):
File "", line 1, in
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

>>> ge.next()
0
>>> ge.next()
1
>>> ge.next()
2
>>> ge.next()
Traceback (most recent call last):
File "", line 1, in
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 "", line 1, in
StopIteration

迭代器和生成器_第6张图片
生成器解析

你可能感兴趣的:(迭代器和生成器)