Python 括号和范围内的变量
Python variable in brackets and range
([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: inconsistent use of tabs and spaces in
indentation
只是缩进不正确。所以,使用文本编辑器检查缩进是否正确。
即将
previous, current = current, [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]
所以,他只是想列一个清单,如上所示
([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: inconsistent use of tabs and spaces in indentation
只是缩进不正确。所以,使用文本编辑器检查缩进是否正确。
即将
previous, current = current, [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]
所以,他只是想列一个清单,如上所示