Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 3中查找字符串中短语第二次出现的索引?_Python_Python 3.x_Indexing - Fatal编程技术网

如何在Python 3中查找字符串中短语第二次出现的索引?

如何在Python 3中查找字符串中短语第二次出现的索引?,python,python-3.x,indexing,Python,Python 3.x,Indexing,如何(在Python 3中)找到字符串中短语第二次出现的索引?到目前为止,我的代码是 result = string.index("phrase im looking for") print (result) 这给了我字符串“string”中“短语im-seeking”的索引。但是,如果“我要查找的短语”出现两次,并且我想查找第二次出现的索引(忽略第一次),我该如何进行此操作?您可以按以下操作查找某个短语的索引,例如: import re mystring = "some p

如何(在Python 3中)找到字符串中短语第二次出现的索引?到目前为止,我的代码是

    result = string.index("phrase im looking for")
    print (result)

这给了我字符串“string”中“短语im-seeking”的索引。但是,如果“我要查找的短语”出现两次,并且我想查找第二次出现的索引(忽略第一次),我该如何进行此操作?

您可以按以下操作查找某个短语的索引,例如:

import re

mystring = "some phrase with some other phrase somewhere"

indices = [s.start() for s in re.finditer('phrase', mystring)]

print(indices)
%[5, 28]

显然,第二次出现的“短语”的索引是
索引[1]

您可以按如下方法查找某个短语的索引,例如:

import re

mystring = "some phrase with some other phrase somewhere"

indices = [s.start() for s in re.finditer('phrase', mystring)]

print(indices)
%[5, 28]

显然,第二次出现的“短语”的索引是
索引[1]

您可以按如下方法查找某个短语的索引,例如:

import re

mystring = "some phrase with some other phrase somewhere"

indices = [s.start() for s in re.finditer('phrase', mystring)]

print(indices)
%[5, 28]

显然,第二次出现的“短语”的索引是
索引[1]

您可以按如下方法查找某个短语的索引,例如:

import re

mystring = "some phrase with some other phrase somewhere"

indices = [s.start() for s in re.finditer('phrase', mystring)]

print(indices)
%[5, 28]

很明显,第二次出现“短语”的索引是
索引[1]

非常棒,效果非常好!你介意解释一下它是如何工作的吗?我的理解如下:索引(变量)被赋予值。。。。(s.start等在做什么?)?谢谢堆人:)太棒了,工作得很完美!你介意解释一下它是如何工作的吗?我的理解如下:索引(变量)被赋予值。。。。(s.start等在做什么?)?谢谢堆人:)太棒了,工作得很完美!你介意解释一下它是如何工作的吗?我的理解如下:索引(变量)被赋予值。。。。(s.start等在做什么?)?谢谢堆人:)太棒了,工作得很完美!你介意解释一下它是如何工作的吗?我的理解如下:索引(变量)被赋予值。。。。(s.start等在做什么?)?感谢堆人:)