<p>我能探测到火柴,但找不到它们在哪里。</p>
<p>给出以下列表:</p>
<pre><code>['A second goldfish is nice and all', 3456, 'test nice']
</code></pre>
<p>我需要搜索match(即“nice”)并打印包含它的所有列表元素。理想情况下,如果要搜索的关键字是“nice”,则结果应该是:</p>
<pre><code>'A second goldfish is nice and all'
'test nice'
</code></pre>
<p>我有:</p>
<pre><code>list = data_array
string = str(raw_input("Search keyword: "))
print string
if any(string in s for s in list):
print "Yes"
</code></pre>
<p>所以它找到匹配项并同时打印,关键字和“是”,但它不告诉我它在哪里。</p>
<p>我应该遍历列表中的每个索引,并为每个迭代搜索“s中的字符串”还是有一种更简单的方法来完成?</p>
<p>试试这个:</p>
<pre><code>list = data_array
string = str(raw_input("Search keyword: "))
print string
for s in list:
if string in str(s):
print 'Yes'
print list.index(s)
</code></pre>
<p>编辑为工作示例。如果只需要第一个匹配索引,也可以在If语句计算为true后中断</p>