【发布时间】:2019-02-19 03:08:35
【问题描述】:
我有一个生成器,我希望得到它。
def foo():
a = map(lambda x: x*2, range(5))
# I want a better way to the next few lines
# The one that looks more functional
for i in a:
yield i
我有地图、过滤器等。我想获得让步,有其他方法吗?我查看了itertools 和functools,找不到任何东西。
编辑:
为了更清楚,我想要一种在每次函数调用时返回一个值的方法。
【问题讨论】:
-
在文档中搜索
yield from -
yield from a也许? -
地图对象不是生成器(也缺少
)) -
链接问题中得分最高的答案包含有关
yield fromstackoverflow.com/a/18620655/4014959的良好信息 -
顺便说一句,我认为这只是一个玩具示例,但使用 lambda 作为
map的函数 arg 是一种代码味道。改为写一个生成表达式。这样您就可以避免每次迭代都调用缓慢的 Python 函数。
标签: python python-3.x generator yield