擅长:python、mysql、java
<p>如果给定正确的输入数据,则函数将按预期工作:</p>
<pre><code>def startsWithSearch(movieList):
searchString = "test4"
for movie in movieList:
if(movie.startswith(searchString)):
print(movie)
startsWithSearch(["test1","testnomatch","test4","test4should","not_test4"])
</code></pre>
<p>输出为:</p>
^{pr2}$
<p>所以一切都正确。。。必须是您的输入数据</p>
<p>我知道你想要一个StartWith解决方案,正如你的函数名所说,但实际上搜索电影,这是非常方便的,如果你在字符串中找到任何匹配,那么如果我搜索“mentalist”,我会找到“the mentalist”,那么你可以使用:</p>
<pre><code>if searchString in movie:
print(movie)
</code></pre>
<p>正如安娜建议的那样,忽略这个案子:</p>
<pre><code>if searchString.lower() in movie.lower():
</code></pre>
<p>或者更喜欢使用正则表达式(第一行需要<code>import re</code>):</p>
<pre><code>if re.match(".*" + searchString,movie,re.I):
</code></pre>
<p>或者如果你真的只想在名字的开头匹配:</p>
<pre><code>if re.match(searchString,movie,re.I):
</code></pre>
<p>这就足够了:)</p>