def subStringMatchExact(target, key):
if (target.find(key) == -1):
return []
else:
foundStringAt = [target.find(key)]
target = target[foundStringAt[0] + len(key):]
return foundStringAt + subStringMatchExact(target, key)
string = subStringMatchExact("your code works with wrongly correlated coefficients which incorporates more costs", "co")
print(string)
Run Code Online (Sandbox Code Playgroud)
当前输出错误:
[5, 22, 9, 19, 14]
Run Code Online (Sandbox Code Playgroud)
我在上一个递归步骤中总结子字符串的长度时遇到了麻烦.就像列表的第二个元素应该29而不是22像len(previousSubstring) + len(key) - 1 + len(currentSubstring).
有什么想法来改进我的代码和/或修复我的错误吗?
您不必实施自己的解决方案,它已经完成了!使用模块中的finditer功能re:
>>> import re
>>> s = 'your code works with wrongly correlated coefficients which incorporates more costs'
>>> matches = re.finditer('co', s)
>>> positions = [ match.start() for match in matches ]
>>> positions
[5, 29, 40, 61, 77]
Run Code Online (Sandbox Code Playgroud)
如果你想自己实现(使用递归),你可以利用str.find函数的额外参数.让我们看看help(str.find)它的内容:
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.
Run Code Online (Sandbox Code Playgroud)
有一个额外的参数叫做start告诉从str.find哪里开始搜索子字符串.这正是我们所需要的!
因此,修改您的实现,我们可以获得一个简单,快速和美观的解决方案:
def substring_match_exact(pattern, string, where_should_I_start=0):
# Save the result in a variable to avoid doing the same thing twice
pos = string.find(pattern, where_should_I_start)
if pos == -1:
# Not found!
return []
# No need for an else statement
return [pos] + substring_match_exact(pattern, string, pos + len(key))
Run Code Online (Sandbox Code Playgroud)
[].[pos]加上子字符串将出现在从位置开始的字符串中的所有位置pos + len(key).>>> s = 'your code works with wrongly correlated coefficients which incorporates more costs'
>>> substring_match_exact('co', s)
[5, 29, 40, 61, 77]
Run Code Online (Sandbox Code Playgroud)