numpy array assignment problem
Posted
by Sujan
on Stack Overflow
See other posts from Stack Overflow
or by Sujan
Published on 2010-06-17T06:33:53Z
Indexed on
2010/06/17
7:13 UTC
Read the original article
Hit count: 224
Hi All:
I have a strange problem in Python 2.6.5 with Numpy. I assign a numpy array, then equate a new variable to it. When I perform any operation to the new array, the original's values also change. Why is that? Please see the example below. Kindly enlighten me, as I'm fairly new to Python, and programming in general.
-Sujan
>>> import numpy as np
>>> a = np.array([[1,2],[3,4]])
>>> b = a
>>> b
array([[1, 2],
[3, 4]])
>>> c = a
>>> c
array([[1, 2],
[3, 4]])
>>> c[:,1] = c[:,1] + 5
>>> c
array([[1, 7],
[3, 9]])
>>> b
array([[1, 7],
[3, 9]])
>>> a
array([[1, 7],
[3, 9]])
© Stack Overflow or respective owner