【发布时间】:2014-02-04 17:36:35
【问题描述】:
我能够检测到匹配项,但无法找到它们的位置。
给定以下列表:
['A second goldfish is nice and all', 3456, 'test nice']
我需要搜索匹配(即“nice”)并打印包含它的所有列表元素。理想情况下,如果要搜索的关键字“不错”,则结果应该是:
'A second goldfish is nice and all'
'test nice'
我有:
list = data_array
string = str(raw_input("Search keyword: "))
print string
if any(string in s for s in list):
print "Yes"
所以它找到匹配项并打印关键字和“是”,但它没有告诉我它在哪里。
我应该遍历列表中的每个索引并为每次迭代搜索“s 中的字符串”还是有更简单的方法来做到这一点?
【问题讨论】:
标签: python string list loops search