1

I have a 2D array A of shape (4,3), and a 1D array a of shape (4,). I want to swap the first two rows of A, as well as the first two elements in a. I did the following:

A[0,:],A[1,:] = A[1,:],A[0,:]
a[0],a[1] = a[1],a[0]

Apparently, it works for a, but fails for A. Now, the second row becomes the first row, but the first row remains unchanged. If I do the following:

first_row_copy = A[0,:].copy()
A[0,:] = A[1,:]
A[1,:] = first_row_copy

Then, it seems to work. Why the first method doesn't work? (but works for a) Also, what's the difference between A_copy = A[0,:].copy() and A_copy = A[0,:]?

1 Answer 1

5

numpy slices are views of the underlying memory, they don't make independent copies by default (this is a performance/memory optimization). So:

A[0,:],A[1,:] = A[1,:],A[0,:]

Makes a view of A[1,:] and a view of A[0,:], then assigns the values of A[0,:] to equal what's in the view of A[1,:]. But when it gets to assigning A[1,:], A[0,:]'s view is now showing the post-copy data, so you get the incorrect result. Simply adding .copy to the second element here would be sufficient in this case:

A[0,:], A[1,:] = A[1,:], A[0,:].copy()

because the tuple on the right is always constructed completely before assignments to the left begin, so you can use the live view for the first assignment, and only need to make a copy to preserve the values for the second assignment.

Sign up to request clarification or add additional context in comments.

2 Comments

then why does it work for a? Because it is not numpy slices and so it is making copy by default?
@Physicist: Plain indexing for a single value makes copies (there is nothing to be gained by avoiding copying the memory in that case).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.