python 中的递归收益率
Yield with recursion in python
我正在尝试使用生成器方法生成两个字符串的集合乘积,而不是将其存储在如下列表中:
def recursive_generator(l_set, index = 0, value = ""):
if index == len(l_set):
yield(value)
else:
for i in l_set[index]:
recursive_generator(l_set, index + 1, value + i)
def main():
_input = ["ab","de"]
for i in recursive_generator(_input):
print(i)
main()
预期结果是 ad ae bd be
换行分隔,但在执行时没有结果。
在检查 pdb 以查看行为时:
$ python3 -m pdb recurse.py
> /home/ec2-user/recurse.py(1)<module>()
-> def recursive_generator(l_set, index = 0, value = ""):
(Pdb) n
> /home/ec2-user/recurse.py(8)<module>()
-> def main():
(Pdb) n
> /home/ec2-user/recurse.py(13)<module>()
-> main()
(Pdb) step
--Call--
> /home/ec2-user/recurse.py(8)main()
-> def main():
(Pdb) n
> /home/ec2-user/recurse.py(9)main()
-> _input = ["ab","de"]
(Pdb) n
> /home/ec2-user/recurse.py(10)main()
-> for i in recursive_generator(_input):
(Pdb) step
--Call--
> /home/ec2-user/recurse.py(1)recursive_generator()
-> def recursive_generator(l_set, index = 0, value = ""):
(Pdb) locals()
{'l_set': ['ab', 'de'], 'index': 0, 'value': ''}
(Pdb) n
> /home/ec2-user/recurse.py(2)recursive_generator()
-> if index == len(l_set):
(Pdb) len(l_set)
2
(Pdb) n
> /home/ec2-user/recurse.py(5)recursive_generator()
-> for i in l_set[index]:
(Pdb) n
> /home/ec2-user/recurse.py(6)recursive_generator()
-> recursive_generator(l_set, index + 1, value + i)
(Pdb) step
--Call--
> /home/ec2-user/recurse.py(1)recursive_generator()
-> def recursive_generator(l_set, index = 0, value = ""):
(Pdb) locals()
{'l_set': ['ab', 'de'], 'index': 1, 'value': 'a'}
(Pdb) n
GeneratorExit <<<<< It did not proceed for the next loop, did not even check the if condition
> /home/ec2-user/recurse.py(1)recursive_generator()
-> def recursive_generator(l_set, index = 0, value = ""):
(Pdb) w
/usr/lib64/python3.7/bdb.py(578)run()
-> exec(cmd, globals, locals)
<string>(1)<module>()
/home/ec2-user/recurse.py(13)<module>()
-> main()
/home/ec2-user/recurse.py(10)main()
-> for i in recursive_generator(_input):
/home/ec2-user/recurse.py(6)recursive_generator()
-> recursive_generator(l_set, index + 1, value + i)
> /home/ec2-user/recurse.py(1)recursive_generator()
-> def recursive_generator(l_set, index = 0, value = ""):
(Pdb) n
Internal StopIteration
> /home/ec2-user/recurse.py(10)main()
-> for i in recursive_generator(_input):
(Pdb) n
--Return--
> /home/ec2-user/recurse.py(10)main()->None
-> for i in recursive_generator(_input):
(Pdb) w
/usr/lib64/python3.7/bdb.py(578)run()
-> exec(cmd, globals, locals)
<string>(1)<module>()
/home/ec2-user/recurse.py(13)<module>()
-> main()
> /home/ec2-user/recurse.py(10)main()->None
-> for i in recursive_generator(_input):
(Pdb) n
--Return--
> /home/ec2-user/recurse.py(13)<module>()->None
-> main()
(Pdb) w
/usr/lib64/python3.7/bdb.py(578)run()
-> exec(cmd, globals, locals)
<string>(1)<module>()->None
> /home/ec2-user/recurse.py(13)<module>()->None
-> main()
我无法理解此处的流程中断为何会执行生成器退出并停止迭代。是否支持这种方法?我是 python 的菜鸟,正在学习生成器和迭代器。任何指导表示赞赏。
你所做的只是在循环中调用 recursive_generator
而不是 yielding
任何值(类似于 return 在非生成器递归函数中调用一个值,你只是return 递归函数调用)。您需要在循环内使用 yield from
来获取生成器函数的递归行为。
def recursive_generator(l_set, index = 0, value = ""):
if index == len(l_set):
yield(value)
else:
for i in l_set[index]:
yield from recursive_generator(l_set, index + 1, value + i)
输出:
ad
ae
bd
be
我正在尝试使用生成器方法生成两个字符串的集合乘积,而不是将其存储在如下列表中:
def recursive_generator(l_set, index = 0, value = ""):
if index == len(l_set):
yield(value)
else:
for i in l_set[index]:
recursive_generator(l_set, index + 1, value + i)
def main():
_input = ["ab","de"]
for i in recursive_generator(_input):
print(i)
main()
预期结果是 ad ae bd be
换行分隔,但在执行时没有结果。
在检查 pdb 以查看行为时:
$ python3 -m pdb recurse.py
> /home/ec2-user/recurse.py(1)<module>()
-> def recursive_generator(l_set, index = 0, value = ""):
(Pdb) n
> /home/ec2-user/recurse.py(8)<module>()
-> def main():
(Pdb) n
> /home/ec2-user/recurse.py(13)<module>()
-> main()
(Pdb) step
--Call--
> /home/ec2-user/recurse.py(8)main()
-> def main():
(Pdb) n
> /home/ec2-user/recurse.py(9)main()
-> _input = ["ab","de"]
(Pdb) n
> /home/ec2-user/recurse.py(10)main()
-> for i in recursive_generator(_input):
(Pdb) step
--Call--
> /home/ec2-user/recurse.py(1)recursive_generator()
-> def recursive_generator(l_set, index = 0, value = ""):
(Pdb) locals()
{'l_set': ['ab', 'de'], 'index': 0, 'value': ''}
(Pdb) n
> /home/ec2-user/recurse.py(2)recursive_generator()
-> if index == len(l_set):
(Pdb) len(l_set)
2
(Pdb) n
> /home/ec2-user/recurse.py(5)recursive_generator()
-> for i in l_set[index]:
(Pdb) n
> /home/ec2-user/recurse.py(6)recursive_generator()
-> recursive_generator(l_set, index + 1, value + i)
(Pdb) step
--Call--
> /home/ec2-user/recurse.py(1)recursive_generator()
-> def recursive_generator(l_set, index = 0, value = ""):
(Pdb) locals()
{'l_set': ['ab', 'de'], 'index': 1, 'value': 'a'}
(Pdb) n
GeneratorExit <<<<< It did not proceed for the next loop, did not even check the if condition
> /home/ec2-user/recurse.py(1)recursive_generator()
-> def recursive_generator(l_set, index = 0, value = ""):
(Pdb) w
/usr/lib64/python3.7/bdb.py(578)run()
-> exec(cmd, globals, locals)
<string>(1)<module>()
/home/ec2-user/recurse.py(13)<module>()
-> main()
/home/ec2-user/recurse.py(10)main()
-> for i in recursive_generator(_input):
/home/ec2-user/recurse.py(6)recursive_generator()
-> recursive_generator(l_set, index + 1, value + i)
> /home/ec2-user/recurse.py(1)recursive_generator()
-> def recursive_generator(l_set, index = 0, value = ""):
(Pdb) n
Internal StopIteration
> /home/ec2-user/recurse.py(10)main()
-> for i in recursive_generator(_input):
(Pdb) n
--Return--
> /home/ec2-user/recurse.py(10)main()->None
-> for i in recursive_generator(_input):
(Pdb) w
/usr/lib64/python3.7/bdb.py(578)run()
-> exec(cmd, globals, locals)
<string>(1)<module>()
/home/ec2-user/recurse.py(13)<module>()
-> main()
> /home/ec2-user/recurse.py(10)main()->None
-> for i in recursive_generator(_input):
(Pdb) n
--Return--
> /home/ec2-user/recurse.py(13)<module>()->None
-> main()
(Pdb) w
/usr/lib64/python3.7/bdb.py(578)run()
-> exec(cmd, globals, locals)
<string>(1)<module>()->None
> /home/ec2-user/recurse.py(13)<module>()->None
-> main()
我无法理解此处的流程中断为何会执行生成器退出并停止迭代。是否支持这种方法?我是 python 的菜鸟,正在学习生成器和迭代器。任何指导表示赞赏。
你所做的只是在循环中调用 recursive_generator
而不是 yielding
任何值(类似于 return 在非生成器递归函数中调用一个值,你只是return 递归函数调用)。您需要在循环内使用 yield from
来获取生成器函数的递归行为。
def recursive_generator(l_set, index = 0, value = ""):
if index == len(l_set):
yield(value)
else:
for i in l_set[index]:
yield from recursive_generator(l_set, index + 1, value + i)
输出:
ad
ae
bd
be