擅长:python、mysql、java
<p>使用module<code>%</code>运算符检查索引是否为偶数。我们可以使用<a href="https://docs.python.org/3/library/functions.html#enumerate" rel="nofollow noreferrer">^{<cd2>}</a>同时获得索引和元素,<a href="https://docs.python.org/3/library/functions.html#any" rel="nofollow noreferrer">^{<cd3>}</a>检查<em>任何</em>元素是否满足条件</p>
<pre><code>>>> s = " a b *"
>>> any(x == '*' for i, x in enumerate(s) if i % 2 == 0)
False
>>> s = "c * b *"
>>> any(x == '*' for i, x in enumerate(s) if i % 2 == 0)
True
</code></pre>