【发布时间】:2020-06-09 17:33:00
【问题描述】:
我发现了一个非常有用的关于 Naive 字符串算法的代码,它是用 python 编写的,但它似乎没有运行有什么问题?
def naive(p, t):
occurrences = []
for i in range(len(t) - len(p) + 1): # loop over alignments
match = True
for j in range(len(p)): # loop over characters
if t[i+j] != p[j]: # compare characters
match = False # mismatch; reject alignment
break #goes back to the outer loop and start the next step of looping in the alignment
if match: #if match remained true
occurrences.append(i) # all chars matched; record
return occurrences
naive("asge","asgefjlso") #to run #但它不运行也不显示任何东西
谁能帮我解释一下 if t[i+j] != p[j] 是什么意思?
【问题讨论】:
-
你能发布示例输入和预期输出吗
-
"我在 Naive 字符串算法上找到了一个非常有用的代码,它是用 python 编写的,但它似乎没有运行"——如果它没有运行,它在什么意义上有用?这就像说“我找到了一辆非常有用的汽车,但我无法驾驶它。”另外,你的问题不清楚。您是在问
!=在 Python 中的含义吗?如果这是您的问题,您可以从基本教程中受益。 -
我的问题是关于这部分如果 t[i+j] , i+j 在内部循环中表示什么?
标签: python python-3.x algorithm search