2 回答
TA贡献1862条经验 获得超6个赞
这是我想出的递归解决方案,希望它是正确的:
def substr_in_str_word(string, substr, edit_distance_tolerance):
if edit_distance_tolerance<0:
return False
if len(substr) == 0:
return True
if len(string) == 0:
return False
for s1 in string:
for s2 in substr:
if s1==s2:
return substr_in_str(string[1:],substr[1:], edit_distance_tolerance)
else:
return substr_in_str(string[1:],substr[1:], edit_distance_tolerance-1) or \
substr_in_str(string[1:],substr[1:], edit_distance_tolerance-1) or\
substr_in_str(string[1:],substr, edit_distance_tolerance-1) or \
substr_in_str(string,substr[1:], edit_distance_tolerance-1)
def substr_in_str(string, substr, edit_distance_tolerance):
for word in string.split(' '):
if substr_in_str_word(word, substr, edit_distance_tolerance):
return True
return False
测试:
str = 'Python is a multi-paradigm'
substr1 = 'ython'
substr2 = 'thon'
substr3 = 'cython'
edit_distance_tolerance = 1
print(substr_in_str(str, substr1, edit_distance_tolerance))
print(substr_in_str(str, substr2, edit_distance_tolerance))
print(substr_in_str(str, substr3, edit_distance_tolerance))
输出:
True
False
True
TA贡献2037条经验 获得超6个赞
答案并不像你想象的那么简单,你需要大量的数学来实现这一点,而标准的 re(regex) 库无法解决这个问题。我认为 TRE 库已经在很大程度上解决了这个问题,请参见这里https://github.com/laurikari/tre/
添加回答
举报