我有一个生成器,想找出它生成的第一个值大于 X。一种方法如下,但它似乎相当冗长(它读起来就像它在重复自己)。
def long_winded(gen,X)
n = next(gen)
while n < X: n=next(gen)
return n
我想写的是更简单的东西:
short_broken(gen,X):
while next(gen)<X: pass
return next(gen) # returns the SECOND value larger than X, as gen is called again
short_broken2(gen,X):
while n = next(gen)<X: pass # Not python syntax!
return n
有没有一种简洁的方式来返回相同的结果?