Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在列表中搜索或查找特定子字符串_Python_List - Fatal编程技术网

Python 如何在列表中搜索或查找特定子字符串

Python 如何在列表中搜索或查找特定子字符串,python,list,Python,List,如何从字符串列表中获取特定字符串?我需要从li[1]字符串中找到goodTime99,以及它的确切位置 if value.find(“goodTime99”)!=-1:如果我给出整个字符串,我知道会有效是否可能\u time100:goodTime99 否则,如何通过搜索goodTime99而不是搜索来精确定位是可能的吗\u time100:goodTime99值。索引(“goodTime99”)给出了一个错误 我不想搜索整个字符串,value.index(“是否可能”\u time100:go

如何从字符串列表中获取特定字符串?我需要从
li[1]
字符串中找到
goodTime99
,以及它的确切位置

if value.find(“goodTime99”)!=-1:
如果我给出整个字符串,我知道会有效
是否可能\u time100:goodTime99

否则,如何通过搜索
goodTime99
而不是搜索
来精确定位是可能的吗\u time100:goodTime99
<代码>值。索引(“goodTime99”)给出了一个错误


我不想搜索整个字符串,
value.index(“是否可能”\u time100:goodTime99”)
很好,但我不想要这个。是否要执行此操作?

您只需依次搜索每个字符串:

value = ["python:guru-age20",
         "is_it_possible_time100:goodTime99",
         "hmm_hope_no_onecanansswer"]

如果您只想检查列表中任何字符串中是否存在
“goodTime99”
,您可以尝试:

for s in value:
  if s.find("goodTime99"):
     print "found in", s
     break
如果您需要确切的位置:

value = ["python:guru-age20", "is_it_possible_time100:goodTime99","hmm_hope_no_onecanansswer"]
if any("goodTime99" in s for s in value):
    # found it
或:


如果你想要一行答案,你可以

def find_first_substring(lst, substring):
    return next((i for i, s in enumerate(lst) if substring in s), -1)

>>> find_first_substring(value, "goodTime99")
1
最简单的方法可以是:

它将为您提供包含列表中的子字符串的字符串的所有索引

In [339]: [value.index(x) for x in value if x.find("goodTime99") > -1]
Out[339]: [1]

这将为您提供所有匹配项以及索引(如果需要):

In [334]: value = ["python:guru-age20",
   .....:          "is_it_possible_time100:goodTime99",
   .....:          "hmm_hope_no_onecanansswer"]

In [335]: indexs = []

In [336]: for x in value:
   .....:     if x.find("goodTime99") > -1:
   .....:         indexs.append(value.index(x))
   .....:

In [337]: print indexs
[1]

In [338]: int(*indexs)
Out[338]: 1
如果您只需要索引,那么

filter(lambda x: x[1].find("goodTime99") != -1, enumerate(value))
例如,我得到的是:

[e[0] for e in filter(lambda x: x[1].find("goodTime99") != -1, enumerate(value))]

s.find
返回一个整数值,指示匹配的开始,
-1
表示未找到
-1
是一个真实值,因此它将始终打印“found”,除非字符串以
goodTime99
开头。如果s:中的“goodTime99”,您希望在测试中使用
。这很好,但我需要stirng li[0]或li[1]的确切位置oot@sr-查询:/#python volume_check.py new_volume Traceback(最后一次调用):文件“volume_check.py”,第23行,在下一行(i代表i,s在枚举(mount_list)中,如果“goodTime99”在s中)停止迭代root@sr-查询:/#Python 2.6.1这是我的version@SRquery我编辑了我的问题,以避免
StopIteration
,我希望它现在更清晰:)@SRquery:it is
indexs
而不是
index
。。。复制粘贴确切的东西是python提示符,你会得到结果。。。检查oneliner答案,这会有所帮助you@avasal,
[value.index(x)for x in value if x.find(“goodTime99”)>-1]Out[339]:[1]
,这不是很有效,因为您将在x的索引中搜索每个匹配的x。最好先使用enumerate生成索引。请参阅:筛选器返回包含匹配元素的列表。@SRquery,您尝试过吗?如果您编写
res=filter(lambda x:x[1]。find(“goodTime99”)!=-1,enumerate(value))
,则res将[(1,'是否可能?'u time100:goodTime99')]与您的上述输入一致
[e[0] for e in filter(lambda x: x[1].find("goodTime99") != -1, enumerate(value))]
>>> value = ["python:guru-age20", "is_it_possible_time100:goodTime99","hmm_hope_no_onecanansswer"]
>>> res = [e[0] for e in filter(lambda x: x[1].find("goodTime99") != -1, enumerate(value))]
>>> res
[1]