擅长:python、mysql、java
<p>一个好的、快速的方法可以将<a href="https://docs.python.org/3/library/functions.html#next" rel="nofollow noreferrer">^{<cd1>}</a>与<a href="https://docs.python.org/3/library/functions.html#enumerate" rel="nofollow noreferrer">^{<cd2>}</a>和<a href="https://www.python.org/dev/peps/pep-0289/" rel="nofollow noreferrer">generator expression</a>一起使用:</p>
<pre><code>next(index for index,item in enumerate(mylist) if item != '')
</code></pre>
<p>这非常有效;一旦找到匹配项,它就会停止检查。如果列表中的所有内容都是<code>''</code>,它将抛出一个StopIteration异常。如果你想要一个默认值,那么说-1:</p>
^{pr2}$