0

What does E = [[0] * m] * n do exactly? I though it would give me an n x m list of lists of 0. I narrowed down the problem so the code has lines that are missing but I am trying to fill the table for edit distance which is in Chapter 6 of Algorithms by Dasgupta et al.

Dasgupta et al

What I wanted to do with the for loop for i was to store the index number in the first column but I am puzzled.

def minDistance(self, word1: str, word2: str) -> int:
        n = len(word1) + 1
        m = len(word2) + 1

        E = [[0] * m] * n

        for i in range(1, n):
            E[i][0] = i

        print('\n'.join(['\t'.join([str(cell) for cell in row]) for row in E])

However, I got 11 which is the last index of i on the entire first column.

11      0       0       0       0       0       0       0       0       0       0
11      0       0       0       0       0       0       0       0       0       0
11      0       0       0       0       0       0       0       0       0       0
11      0       0       0       0       0       0       0       0       0       0
11      0       0       0       0       0       0       0       0       0       0
11      0       0       0       0       0       0       0       0       0       0
11      0       0       0       0       0       0       0       0       0       0
11      0       0       0       0       0       0       0       0       0       0
11      0       0       0       0       0       0       0       0       0       0
11      0       0       0       0       0       0       0       0       0       0
11      0       0       0       0       0       0       0       0       0       0
11      0       0       0       0       0       0       0       0       0       0

I made a substitution to E = [[0] * m for _ in range(n)] following code from this question and got the right answer.

0       0       0       0       0       0       0       0       0       0       0
1       0       0       0       0       0       0       0       0       0       0
2       0       0       0       0       0       0       0       0       0       0
3       0       0       0       0       0       0       0       0       0       0
4       0       0       0       0       0       0       0       0       0       0
5       0       0       0       0       0       0       0       0       0       0
6       0       0       0       0       0       0       0       0       0       0
7       0       0       0       0       0       0       0       0       0       0
8       0       0       0       0       0       0       0       0       0       0
9       0       0       0       0       0       0       0       0       0       0
10      0       0       0       0       0       0       0       0       0       0
11      0       0       0       0       0       0       0       0       0       0
heretoinfinity
  • 1,308
  • 3
  • 11
  • 29
  • 2
    Does this answer your question? [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – python_user Mar 05 '21 at 03:35
  • 1
    "I though it would give me an n x m list of lists of 0". Yes, but when you do it this way, it creates a list with `n` references *to the same list object*, which was created with the inner `[0]*m` (which is itself, a list with `m` references to the same `int` object). Consider, `x = [0, 0, 0]` then if you did `[x]*3` it would be equivalent to `[x, x, x]`. – juanpa.arrivillaga Mar 05 '21 at 03:42
  • I don't follow. If I set `y = [x]*3` and then do `x=2`, I am still left with `y = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]` – heretoinfinity Mar 05 '21 at 12:38

0 Answers0