1

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?

lovespeed
  • 4,835
  • 15
  • 41
  • 54

1 Answers1

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