这是什么意思 ([i]+[0]*n),为什么括号里是i和0??
previous, current = current, [i]+[0]*n
为什么我不能在下一行打印当前值?像这样:
previous, current = current, [i]+[0]*n
print(current)
我有一个错误:TabError:在缩进中使用不一致的制表符和空格
#!/usr/bin/env python
# This is a straightforward implementation of a well-known algorithm, and thus
# probably shouldn't be covered by copyright to begin with. But in case it is,
# the author (Magnus Lie Hetland) has, to the extent possible under law,
# dedicated all copyright and related and neighboring rights to this software
# to the public domain worldwide, by distributing it under the CC0 license,
# version 1.0. This software is distributed without any warranty. For more
# information, see <http://creativecommons.org/publicdomain/zero/1.0>
def levenshtein(a,b):
"Calculates the Levenshtein distance between a and b."
n, m = len(a), len(b)
if n > m:
# Make sure n <= m, to use O(min(n,m)) space
a,b = b,a
n,m = m,n
current = range(n+1)
for i in range(1,m+1):
previous, current = current, [i]+[0]*n
for j in range(1,n+1):
add, delete = previous[j]+1, current[j-1]+1
change = previous[j-1]
if a[j-1] != b[i-1]:
change = change + 1
current[j] = min(add, delete, change)
return current[n]
if __name__=="__main__":
from sys import argv
print(levenshtein(argv[1],argv[2]))
以下错误
TabError:在缩进中使用不一致的制表符和空格
只是意味着缩进不正确。因此,请使用文本编辑器并检查缩进是否正确。
来到
前一个,当前 = 当前,[i]+[0]*n
这是在给定代码的以下 for 循环中
for i in range(1,m+1):
previous, current = current, [i]+[0]*n
所以, i 是索引(或计数器变量),他正在做的是制作一个列表,该列表将第一个元素作为索引,然后是 n 个零。这里 n 是在以下代码中计算的第一个字符串的长度
n, m = len(a), len(b)
因此,例如,如果 n = 10 且 i = 1,[i] + [0]*n
则将是
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
所以,他只是想列一个如上所示的清单
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句