擅长:python、mysql、java
<p>下面呢?</p>
<pre><code>print testlist.index(element)
</code></pre>
<p>如果不确定要查找的元素是否在列表中,可以添加一个初步检查,如</p>
<pre><code>if element in testlist:
print testlist.index(element)
</code></pre>
<p>或者</p>
<pre><code>print(testlist.index(element) if element in testlist else None)
</code></pre>
<p>或者“pythonic方式”,我不太喜欢这种方式,因为代码不太清晰,但有时效率更高</p>
<pre><code>try:
print testlist.index(element)
except ValueError:
pass
</code></pre>