Reputation: 21
I want to create two arrays a
and b
, then create an array c
which is the same as a
but the first column is pulled from b
. At the end there should be 3 different arrays.
Something like the following:
a = np.array([[1, 1]])
b = np.array([[0, 0]])
c = b
c[:, 0] = a[:, 0]
print(a, b, c)
Should outputs:
[[1 1]] [[0 0]] [[1 0]]
But I am getting:
[[1 1]] [[1 0]] [[1 0]]
For some reason, modifying c
also modifies a
.
Upvotes: 2
Views: 46
Reputation: 2873
You can perform a copy of the object by using .copy()
like so:
a = np.array([[1, 1]])
b = np.array([[0, 0]])
c = b.copy() # or b.deepcopy()
From the documentation:
Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other. [The copy] module provides generic shallow and deep copy operations...
Link: https://docs.python.org/3.8/library/copy.html
Upvotes: 3