python查找方法没有';t为“工作”/&引用;在这里

python查找方法没有';t为“工作”/&引用;在这里,python,python-2.7,Python,Python 2.7,考虑下面的代码 #!/usr/bin/python url = "http://stackoverflow.com/questions/22389923/python-find-method-doesnt-work-for-here" print url.find("/",8) 您将得到的输出是24,但答案必须是3。不是吗?这将查找子字符串/的第一个索引,从索引8开始搜索 您可能认为这是在计算出现的次数,而不是查找位置,但如果您阅读docstring,您不会误解这一点: Docstring:

考虑下面的代码

#!/usr/bin/python
url = "http://stackoverflow.com/questions/22389923/python-find-method-doesnt-work-for-here"
print url.find("/",8)

您将得到的输出是24,但答案必须是3。不是吗?

这将查找子字符串
/
的第一个索引,从索引8开始搜索

您可能认为这是在计算出现的次数,而不是查找位置,但如果您阅读docstring,您不会误解这一点:

Docstring:
S.find(sub [,start [,end]]) -> int

Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end].  Optional
arguments start and end are interpreted as in slice notation.

Return -1 on failure.
现在,为了得到“3”,我想你可能正在寻找:

>>> url[8:].count('/')
3

这将查找子字符串
/
的第一个索引,从索引8开始搜索

您可能认为这是在计算出现的次数,而不是查找位置,但如果您阅读docstring,您不会误解这一点:

Docstring:
S.find(sub [,start [,end]]) -> int

Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end].  Optional
arguments start and end are interpreted as in slice notation.

Return -1 on failure.
现在,为了得到“3”,我想你可能正在寻找:

>>> url[8:].count('/')
3

输出是正确的,您为什么期望
3
?看:

http://stackoverflow.com/questions/22389923/python-find-method-doesnt-work-for-here
^  ^    ^               ^
0  3    8              24
根据
url.find(“/”,8)
正在查找第8个索引之后第一次出现的
“/”
的索引,该索引恰好位于第24个索引中。引用文件(我的重点):

返回s中找到子字符串sub的最低索引,使sub完全包含在
s[start:end]
中。故障时返回
-1
。开始和结束以及负值解释的默认值与切片的默认值相同

也许你想用它


输出是正确的,您为什么期望
3
?看:

http://stackoverflow.com/questions/22389923/python-find-method-doesnt-work-for-here
^  ^    ^               ^
0  3    8              24
根据
url.find(“/”,8)
正在查找第8个索引之后第一次出现的
“/”
的索引,该索引恰好位于第24个索引中。引用文件(我的重点):

返回s中找到子字符串sub的最低索引,使sub完全包含在
s[start:end]
中。故障时返回
-1
。开始和结束以及负值解释的默认值与切片的默认值相同

也许你想用它


Python中的
find
方法返回字符串中特定字符的索引。可选参数之一是要开始的字符串中的位置。在你的命令中,你说:

print url.find("/", 8)
您告诉它打印斜杠第一次出现的索引,从第8个字符开始。在这个字符串中,出现在字符24上

从文档中:

string.find(s, sub[, start[, end]])

Return the lowest index in s where the substring sub is found such that sub 
is wholly contained in s[start:end]. Return -1 on failure. Defaults for start 
and end and interpretation of negative values is the same as for slices.
更多信息请参见此处的文档:

相反,您似乎在尝试查找某个字符在起始点之后出现的次数。为此,可以使用
.count
方法。下面是一些示例代码

#!/usr/bin/python
url = "http://stackoverflow.com/questions/22389923/python-find-method-doesnt-work-for-here"
print url.count( '/', 8)
# should print 3

这里的文档中有更多内容:

Python中的
find
方法返回字符串中特定字符的索引。可选参数之一是要开始的字符串中的位置。在你的命令中,你说:

print url.find("/", 8)
您告诉它打印斜杠第一次出现的索引,从第8个字符开始。在这个字符串中,出现在字符24上

从文档中:

string.find(s, sub[, start[, end]])

Return the lowest index in s where the substring sub is found such that sub 
is wholly contained in s[start:end]. Return -1 on failure. Defaults for start 
and end and interpretation of negative values is the same as for slices.
更多信息请参见此处的文档:

相反,您似乎在尝试查找某个字符在起始点之后出现的次数。为此,可以使用
.count
方法。下面是一些示例代码

#!/usr/bin/python
url = "http://stackoverflow.com/questions/22389923/python-find-method-doesnt-work-for-here"
print url.count( '/', 8)
# should print 3

这里的文档中有更多信息:

您误解了。它查找某个子字符串的
索引
(即其位置),而不是您希望的发生次数。你想用(惊喜,惊喜)

例如:

>>> url = "http://stackoverflow.com/questions/22389923/python-find-method-doesnt-work-for-here"
>>> url.count('/', 8)
3

这似乎是您想要的输出。

您误解了。它查找某个子字符串的
索引
(即其位置),而不是您希望的发生次数。你想用(惊喜,惊喜)

例如:

>>> url = "http://stackoverflow.com/questions/22389923/python-find-method-doesnt-work-for-here"
>>> url.count('/', 8)
3

这似乎是您想要的输出。

请先阅读。请先阅读。@user3393168
str.find
返回索引,而不是计数。@user3393168
str.find
返回索引,而不是计数。