两个连续的 yield 语句如何在 python 中工作?
How two consecutive yield statement work in python?
我在 pymotw.com 的合并和拆分部分偶然发现了这段代码。
from itertools import *
def make_iterables_to_chain():
yield [1, 2, 3]
yield ['a', 'b', 'c']
for i in chain.from_iterable(make_iterables_to_chain()):
print(i, end=' ')
print()
我不明白 make_iterables_to_chain() 是如何工作的。它包含两个 yield 语句,它是如何工作的?
我知道生成器是如何工作的,但是那里只有一个 yield 语句。
请帮忙!
与单个 yield
的工作方式相同。
您可以在生成器中拥有任意数量的 yield
,当在其上调用 __next__
时,它将执行直到遇到下一个 yield。然后您返回生成的表达式,生成器暂停,直到它的 __next__
方法再次被调用。
运行 几次 next
调用生成器以查看此内容:
>>> g = make_iterables_to_chain() # get generator
>>> next(g) # start generator, go to first yield, get result
[1, 2, 3]
>>> next(g) # resume generator, go to second yield, get result
['a', 'b', 'c']
>>> # next(g) raises Exception since no more yields are found
生成器有效地允许函数return 多次。每执行一次yield
语句,值就return传给调用者,调用者可以继续函数的执行。
通常,它们在 for
循环中用作可迭代对象。
以下函数将可迭代对象中的每个元素递增一个数量:
def inc_each(nums, inc):
for i in nums:
yield i + inc
这是一个用法示例:
gen = inc_each([1, 2, 3, 4], 100)
print(list(gen)) # [101, 102, 103, 104]
list
用于将任意可迭代对象(在本例中为生成器)转换为列表。
您描述的函数执行两个 yield 语句:
def make_iterables_to_chain():
yield [1, 2, 3]
yield ['a', 'b', 'c']
如果你调用它,它 return 是一个生成器,如果遍历,生成列表 [1, 2, 3]
和 ['a', 'b', 'c']
。
gen = make_iterables_to_chain()
print(list(gen)) # [[1, 2, 3], ['a', 'b', 'c']]
itertools.chain.from_iterable
将接受一个(可能是无限的)可迭代对象和 "flatten" 它,return 一个(可能是无限的)可迭代对象作为结果。
这是一种实现方式:
def from_iterable(iterables):
for iterable in iterables:
for i in iterable:
yield i
我在 pymotw.com 的合并和拆分部分偶然发现了这段代码。
from itertools import *
def make_iterables_to_chain():
yield [1, 2, 3]
yield ['a', 'b', 'c']
for i in chain.from_iterable(make_iterables_to_chain()):
print(i, end=' ')
print()
我不明白 make_iterables_to_chain() 是如何工作的。它包含两个 yield 语句,它是如何工作的? 我知道生成器是如何工作的,但是那里只有一个 yield 语句。
请帮忙!
与单个 yield
的工作方式相同。
您可以在生成器中拥有任意数量的 yield
,当在其上调用 __next__
时,它将执行直到遇到下一个 yield。然后您返回生成的表达式,生成器暂停,直到它的 __next__
方法再次被调用。
运行 几次 next
调用生成器以查看此内容:
>>> g = make_iterables_to_chain() # get generator
>>> next(g) # start generator, go to first yield, get result
[1, 2, 3]
>>> next(g) # resume generator, go to second yield, get result
['a', 'b', 'c']
>>> # next(g) raises Exception since no more yields are found
生成器有效地允许函数return 多次。每执行一次yield
语句,值就return传给调用者,调用者可以继续函数的执行。
通常,它们在 for
循环中用作可迭代对象。
以下函数将可迭代对象中的每个元素递增一个数量:
def inc_each(nums, inc):
for i in nums:
yield i + inc
这是一个用法示例:
gen = inc_each([1, 2, 3, 4], 100)
print(list(gen)) # [101, 102, 103, 104]
list
用于将任意可迭代对象(在本例中为生成器)转换为列表。
您描述的函数执行两个 yield 语句:
def make_iterables_to_chain():
yield [1, 2, 3]
yield ['a', 'b', 'c']
如果你调用它,它 return 是一个生成器,如果遍历,生成列表 [1, 2, 3]
和 ['a', 'b', 'c']
。
gen = make_iterables_to_chain()
print(list(gen)) # [[1, 2, 3], ['a', 'b', 'c']]
itertools.chain.from_iterable
将接受一个(可能是无限的)可迭代对象和 "flatten" 它,return 一个(可能是无限的)可迭代对象作为结果。
这是一种实现方式:
def from_iterable(iterables):
for iterable in iterables:
for i in iterable:
yield i