I wrote the following code, where two identical arrays h and h1 are defined in different ways.
Then I produce the same operations on them and print the modified arrays. To my surprise, the outcome is different. The outcome for h1 is what I was expecting for both.
Please help me to understand why.
Here is the code:
# arrays and the operations on them are the same ?
The outcome:
True
[[99, 0, 0], [99, 0, 0], [99, 0, 0]]
[[1, 0, 0], [99, 0, 0], [99, 0, 0]]
Then I produce the same operations on them and print the modified arrays. To my surprise, the outcome is different. The outcome for h1 is what I was expecting for both.
Please help me to understand why.
Here is the code:
l1=3 l2=3 h=l1*[l2*[0]] h1=[[0, 0, 0], [0, 0, 0], [0, 0, 0] ] print(h==h1) #operations on array h for i in range(3): for j in range(3): if i==0: h[0][0]=1 elif j==0: h[i][j]=99 else: pass print(h) #IDENTICAL operations on array h1 for i in range(3): for j in range(3): if i==0: h1[0][0]=1 elif j==0: h1[i][j]=99 else: pass print(h1)#QUESTION: Why print(h) and print(h1) result in different arrays, when the initial
# arrays and the operations on them are the same ?
The outcome:
True
[[99, 0, 0], [99, 0, 0], [99, 0, 0]]
[[1, 0, 0], [99, 0, 0], [99, 0, 0]]