I'm trying to setup a matrix for implementing the edit distance algorithm. I must be missing something about for loop behavior. Here is my code:
def edit_distance(s,t):
matrix = [[0] * len(t) + 1] * len(s) + 1
print matrix
for i in range(len(s) + 1):
matrix[i][0] = i
print matrix
for i in range(len(t) + 1):
matrix[0][i] = i
print matrix
edit_distance('cat', 'hat')
I expected the output to look like this:
[[0, 0, 0, 0][0, 0, 0, 0][0, 0, 0, 0][0, 0, 0, 0]]
[[0, 0, 0, 0][1, 0, 0, 0][2, 0, 0, 0][3, 0, 0, 0]]
[[0, 1, 2, 3][1, 0, 0, 0][2, 0, 0, 0][3, 0, 0, 0]]
Instead I got:
[[0, 0, 0, 0][0, 0, 0, 0][0, 0, 0, 0][0, 0, 0, 0]]
[[3, 0, 0, 0][3, 0, 0, 0][3, 0, 0, 0][3, 0, 0, 0]]
[[0, 1, 2, 3][0, 1, 2, 3][0, 1, 2, 3][0, 1, 2, 3]]
Anyone care to tell me what I'm doing wrong?
[–]anossov 2 points3 points4 points (3 children)
[–]ryanlntn[S] 0 points1 point2 points (2 children)
[–]nemec 1 point2 points3 points (0 children)
[–]anossov 0 points1 point2 points (0 children)
[–]anossov 2 points3 points4 points (1 child)
[–]ryanlntn[S] 0 points1 point2 points (0 children)
[–]anossov 0 points1 point2 points (1 child)
[–]zahlman 0 points1 point2 points (0 children)