python - Difference in behavior between basic type (int) and complex type (np.ndarray) -


why following objects behaving differently regard [x] * n operation? i.e. why first operation (in[94]) modify single entry of list, whereas second operation (in[99]) modify all entries?

in [91]: x = 8  in [92]: y = [x] * 10  in [93]: y out[93]: [8, 8, 8, 8, 8, 8, 8, 8, 8, 8]  in [94]: y[1] = 4  in [95]: y out[95]: [8, 4, 8, 8, 8, 8, 8, 8, 8, 8]  in [96]: x = np.zeros(shape=(3,3))  in [97]: y = [x] * 10  in [98]: y out[98]:  [array([[ 0.,  0.,  0.],         [ 0.,  0.,  0.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  0.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  0.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  0.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  0.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  0.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  0.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  0.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  0.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  0.],         [ 0.,  0.,  0.]])]  in [99]: y[1][1,2] = 5  in [100]: y out[100]:  [array([[ 0.,  0.,  0.],         [ 0.,  0.,  5.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  5.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  5.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  5.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  5.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  5.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  5.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  5.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  5.],         [ 0.,  0.,  0.]]), array([[ 0.,  0.,  0.],         [ 0.,  0.,  5.],         [ 0.,  0.,  0.]])] 

tl;dr : implicitly made shallow copy of matrix when expecting deep copy. documentation

consider following bit of code:

in [38]: import numpy  in [39]: = numpy.eye(2)  in [40]: out[40]: array([[ 1.,  0.],        [ 0.,  1.]])  in [41]: b =  in [42]: b out[42]: array([[ 1.,  0.],        [ 0.,  1.]])  in [43]: a[0,0] = 2  in [44]: out[44]: array([[ 2.,  0.],        [ 0.,  1.]])  in [45]: b out[45]: array([[ 2.,  0.],        [ 0.,  1.]]) 

also:

in [46]: id(a) out[46]: 140529107552512  in [47]: id(b) out[47]: 140529107552512 

i defined a. set b=a , changed a find same change in b though didn't touch b.

by line [x] * 10, made 10 shallow copies of x. so, change in 1 reflects on others.

in [90]: x = np.zeros(shape=(3,3)) in [95]: y = [x] * 10  in [96]: id(y[0]) out[96]: 140529110575952  in [97]: id(y[1]) out[97]: 140529110575952  in [98]: y[1][1,2] = 5  in [99]: id(y[0]) out[99]: 140529110575952  in [100]: id(y[1]) out[100]: 140529110575952 

this not problem int or any other non-compound type.

in [105]: x = 0  in [106]: y = [x] * 10  in [107]: y out[107]: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  in [108]: y[1] = 2  in [109]: y out[109]: [0, 2, 0, 0, 0, 0, 0, 0, 0, 0]  in [110]: id(y[0]) out[110]: 140529069263200  in [111]: id(y[1]) out[111]: 140529069263152 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

c# - Retrieve google contact -

javascript - How to insert selected radio button value into table cell -