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!