My problem is as follows. I have a 2D array. From the 2D array I take out one row as an 1D array and work with it and make changes, but it also changes the original entries in the 2D array, but I want them to remain constant. How do I solve this in python?
Asked
Active
Viewed 1,183 times
1
-
possible duplicate of [How to clone a list in python?](http://stackoverflow.com/questions/2612802/how-to-clone-a-list-in-python) – Rohit Jain Dec 01 '12 at 21:55
-
4@RohitJain: That question is about `list`, and this one is about `ndarray`. – NPE Dec 01 '12 at 21:57
-
@NPE.. Yeah I noticed that. But I thought it can be done in the same way. Couldn't it? – Rohit Jain Dec 01 '12 at 21:59
1 Answers
3
You could use ndarray.copy()
:
In [17]: A = array([[1, 1], [3, 2], [-4, 1]])
In [18]: b = A[1].copy()
In [19]: b
Out[19]: array([3, 2])
In [20]: b[0] = 4
In [21]: b
Out[21]: array([4, 2])
In [22]: A
Out[22]:
array([[ 1, 1],
[ 3, 2],
[-4, 1]])
As you can see, A[1]
remains unchanged.

NPE
- 486,780
- 108
- 951
- 1,012