0

I wrote the following code in python. Within checkd, when I update d[ii][jj], it seems as if the compiler takes its own liberties and makes all the following column entries to 1.

Code:

def checkd(d, level, a, b):
i = len(b)
j = len(a)
print ['0'] + list(a)
for ii in range(i):
    for jj in range(j):
        if a[jj] == b[ii]:
            #print a[jj] +" "+ b[ii] + " Matched."
            d[ii][jj] = 1
        print b[ii] + "\t" + a[jj] + "\t" + str(d[ii][jj])
    print [b[ii]] + [str(m) for m in d[ii]]
return d


a = raw_input("First word:")
b = raw_input("Second word:")
w = input("Size of words to check:")

d = [[0]*len(a)]*len(b)

d = checkd(d, w, a, b)
print d
for x in d : print x

Output:

First word:ascend
Second word:nd
Size of words to check:2
['0', 'a', 's', 'c', 'e', 'n', 'd']
n   a   0
n   s   0
n   c   0
n   e   0
n   n   1
n   d   0
['n', '0', '0', '0', '0', '1', '0']
d   a   0
d   s   0
d   c   0
d   e   0
d   n   1
d   d   1
['d', '0', '0', '0', '0', '1', '1']
[[0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 1, 1]]
[0, 0, 0, 0, 1, 1]
[0, 0, 0, 0, 1, 1]

As you would notice, not only is this leading to some random match(d,n,1?!) in the "d" row, the returned 2d array is just a copy of the last row of the one in the function.

I have some experience with Python. I am not looking for a workaround (not that I'd mind) as much as an explanation for this behaviour, if possible?

Thanks!

1 Answers1

0

This makes a list of len(b) references to the same list of len(a) zeros. A total of two interconnected lists are created.

d = [[0] * len(a)] * len(b)

What you want to do is:

d = [[0] * len(a) for _ in b]

ints are immutable, so they are safe to duplicate like that.

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
  • Thank you very much. I did find the answer just minutes after: http://stackoverflow.com/questions/240178/unexpected-feature-in-a-python-list-of-lists – Aditya Gupta Feb 22 '13 at 10:14