Search code examples
pythonequalsnumeric

the code does not give the results what I want using equal sign in python


I do have a code like this.

import numpy as np
a = np.zeros(shape = (4,4))
a+= 2
b = np.zeros(shape = (4,4))
b+=2
t = 0
while t<2:
    for i in range(1,3):
        for j in range(1,3):
            if a[i,j] == a[i-1,j]:
                b[i,j] = a[i,j]+1
    print(a,t)
    print(b,t)
    a = b
    t+= 1 

I am hoping that at t = 2 a = [2 2 2 2, 2 3 3 2, 2 3 3 2, 2 2 2 2] and b = [2 2 2 2, 2 3 3 2, 2 4 4 2, 2 2 2 2] but in fact at the end of the run the a = [2 2 2 2, 2 3 3 2, 2 4 4 2, 2 2 2 2]

anyone know why? is it because i am declaring a = b? if it is yes, is there any way to do it? thanks..


Solution

  • Replace a=b (which makes a and b the same array) with a[:,:]=b (which copies the elements of b into a).