Search code examples
pythonlevenshtein-distance

Python variable in brackets and range


What is that mean ([i]+[0]*n) and why is i and 0 in the brackets??

previous, current = current, [i]+[0]*n

And why can't I print current value in the next line? Like so:

previous, current = current, [i]+[0]*n
print(current)

I have an error: TabError: inconsistent use of tabs and spaces in indentation

#!/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]))

Solution

  • The following error

    TabError: inconsistent use of tabs and spaces in indentation

    just means that the indentations are not correct. so, use a text editor and check if the indentations are correct.

    Coming to

    previous, current = current, [i]+[0]*n

    which is in the following for loop in the code given

    for i in range(1,m+1):
            previous, current = current, [i]+[0]*n
    

    so, i is the index(or a counter variable) and what he is doing is making a list which will have first element as index and n number of zeros followed. here n is the length of the first string which is evaluated in the following code

    n, m = len(a), len(b)
    

    so, for example if n = 10 and i = 1, then [i] + [0]*n will be

    [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    

    So, he is just trying to make a list as shown above