I was having problems with "equal" arrays being treated differently in python 3.X.
I noticed this when I was working on creating an NxN array dynamically. The array I generated didn't work in my function, but when I typed out a 2D array filled with numbers to see exactly where my code was breaking my function worked with the manually created array.
Here, the arrays are created and seem to be exactly the same.
x = [[0,0,0],[0,0,0],[0,0,0]]
n = 3
a = [[0]*n]*n
print(x)
print(a)
will return...
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Now if I send these through a function I created, even though they appear the same, I get different results.
def checkerboard(a):
'''takes nxn array and returns a checkerboard pattern'''
for i in range(len(a)):
for j in range(len(a[i])):
if((i+j) % 2):
a[i][j] = "X"
else:
a[i][j] = "0"
for i in range(len(a)):
print(a[i])
return a
checkerboard(x)
print("-----------")
checkerboard(a)
gives the result...
['0', 'X', '0']
['X', '0', 'X']
['0', 'X', '0']
-----------
['0', 'X', '0']
['0', 'X', '0']
['0', 'X', '0']
both of these NxN matrices should be the same and should give the same result.