是否有更 pythonic 的方式,或者至少是更短更简单的方式来做到这一点:
i = 1
while True:
res = lookup(i) # returns a value or None
if res is None:
break
else:
i += 1
yield res
最佳答案
您可以使用 itertools
:
from itertools import takewhile, count
# ...
def myfunc():
return takewhile(lambda x: x is not None, (lookup(i) for i in count(1)))
如果您出于某种原因不喜欢takewhile
:
for i in count(1):
res = lookup(i)
if res is None: break
yield res
关于python - for(i=0;True;i++) 在 python 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10785218/