why x is changed in the following program?

Veek. M vek.m1234 at gmail.com
Fri Jul 1 23:35:30 EDT 2016


maurice.charbit at telecom-paristech.fr wrote:

> from numpy import random
> x=random.randn(6)
> y=x
> y[0]=12
> print x[0]
> 
> 
> 

random.rand returns a list. x is a label to this list (container).
y=x creates another label to the same container/list.

y[0[ = 12 alters the 0th position of the container.
print x[0], uses a different label 'x' to change the same container.

use: y = list(x) to do a 'shallow copy' of x into a new list container



More information about the Python-list mailing list