substitute misunderstanding

numpy array に対する通常の代入操作は,いわばリンクをつくるだけで実体はひとつなので,代入後のnumpy arrayを新しいメモリ上にとるためには,copyを使う必要がある.

>a=np.array([1,2,3])

>b=a

>c=a.copy()

>a[0]=10 # An element of a is changed.

>a

array([10, 2, 3])

>b

array([10, 2, 3]) # The element of b, produced by a substitution, is also changed.

>c

array([1, 2, 3]) # The element of c, produced by copy, is not changed.

リストにはimport copy をして,copy.copyを,辞書などにはcopy.deepcopyを使って,新しい実体を作ることができる.

The substitution operation to numpy array produces a "link" that refers the same memory. If you want to produce a numpy array, disconnected from the original one, you need to use copy command.

>a=np.array([1,2,3])

>b=a

>c=a.copy()

>a[0]=10 # An element of a is changed.

>a

array([10, 2, 3])

>b

array([10, 2, 3]) # The element of b, produced by a substitution, is also changed.

>c

array([1, 2, 3]) # The element of c, produced by copy, is not changed.

To produce a new list (dictionary), you can use copy.copy (deepcopy.copy).