擅长:python、mysql、java
<p>您正在查找<code>yield</code>关键字,该关键字生成一个生成器:</p>
<pre><code>def get_bad_results(person_results):
for i in person_results:
if i[1]>i[3] or i[1]<i[2]:
yield i[0]
</code></pre>
<p>然后可以迭代函数的结果(例如,调用<code>list()</code>来创建列表)。生成器比直接生成列表更可取,因为它意味着值是惰性地生成的,这在很多情况下更节省内存。你知道吗</p>
<p>顺便说一句,索引<code>i</code>有点难看,在这种情况下,解压这些值将使代码更具可读性:</p>
<pre><code>def get_bad_results(person_results):
for name, age, height, weight in person_results:
if age > weight or age < height:
yield name
</code></pre>
<p>(当然,我在这里编了名字,因为我不知道上下文)。你知道吗</p>