擅长:python、mysql、java
<p>你在找<a href="http://docs.python.org/2/library/itertools.html#itertools.takewhile" rel="nofollow">itertools.takewhile</a>?在</p>
<pre><code>>>> from itertools import takewhile
>>> def gen(): #infinite generator
... i=0
... while True:
... yield range(i,i+5)
... i = i+1
...
>>> [ x for x in takewhile( lambda x:sum(x)<20, gen() ) ]
[[0, 1, 2, 3, 4], [1, 2, 3, 4, 5]]
>>>
</code></pre>