擅长:python、mysql、java
<p>要在alist中查找单词的索引,请使用<code>.index()</code>函数,并且为了安全起见,在找不到单词时退出代码,请使用异常。显示下图:</p>
<pre><code>bigstring = "I have trouble doing this assignment"
mywords = bigstring.split()
def FindIndexOfWord(list,word):
try:
print(list.index(word))
except ValueError:
print(word," not in list.")
FindIndexOfWord(mywords,"have")
</code></pre>
<p>输出:</p>
<pre><code>1
</code></pre>