[英]Nested generators and yield from?
我有一个嵌套对象, a
,并希望在每个子容器上应用某种类型的过滤器:
a = [[1, 2], [3, 4, 5, 6], [7, 7, 8]]
保持嵌套结构但仅过滤偶数元素的函数如下所示:
def nested_filter(obj):
res = []
for sub_obj in obj:
even = [i for i in sub_obj if i % 2 == 0]
res.append(even)
return res
nested_filter(a)
# [[2], [4, 6], [8]]
不过,我想创建一个使用某种形式的嵌套同等发电机yield
保持嵌套结构a
。
此,为了说明,正是我不想要的东西,因为它变平a
:
def nested_yield(obj):
"""Will flatten the nested structure of obj."""
for sub_obj in obj:
for i in sub_obj:
if i % 2 == 0:
yield i
list(nested_yield(a))
# [2, 4, 6, 8]
我的理解是, Python 3.3中引入的 yield from
可能允许嵌套保留传递对象的结构。 但显然我误解了:
def gen1(obj):
for sub_obj in obj:
yield from gen2(sub_obj)
def gen2(sub_obj):
for i in sub_obj:
if i % 2 == 0:
yield i
list(gen1(a))
# [2, 4, 6, 8]
那么,有什么方法可以使用两个生成器函数(即不创建中间列表),以便调用生成器会给我这样的:
list(nested_generator(a))
# [[2], [4, 6], [8]]
您希望nested_generator
返回一个子列表列表。 在这种情况下,您无法在内层创建临时列表。
因此,您最好的选择是产生过滤后的列表:
>>> def nested_filter(L):
for l in L:
yield [i for i in l if i % 2 == 0]
>>> a = [[1, 2], [3, 4, 5, 6], [7, 7, 8]]
>>> list(nested_filter(a))
[[2], [4, 6], [8]]
如果你想使用第二个发电机来筛选子列表,你可以转换迭代器返回到列表中,并yield
这还有:
>>> def generator_filter(L):
for n in L:
if n % 2 == 0:
yield n
>>> def nested_filter(L):
for l in L:
yield list(generator_filter(l))
>>> a = [[1, 2], [3, 4, 5, 6], [7, 7, 8]]
>>> list(nested_generator(a))
[[2], [4, 6], [8]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.