Swap slices of Numpy arrays Swap slices of Numpy arrays python python

Swap slices of Numpy arrays


Python correctly interprets the code as if you used additional variables, so the swapping code is equivalent to:

t1 = b[:,0,0]t2 = a[:,0,0]a[:,0,0] = t1b[:,0,0] = t2

However, even this code doesn't swap values correctly! This is because Numpy slices don't eagerly copy data, they create views into existing data. Copies are performed only at the point when slices are assigned to, but when swapping, the copy without an intermediate buffer destroys your data. This is why you need not only an additional variable, but an additional numpy buffer, which general Python syntax can know nothing about. For example, this works as expected:

t = np.copy(a[:,0,0])a[:,0,0] = b[:,0,0]b[:,0,0] = t


I find this the simplest:

a[:,0,0], b[:, 0, 0] = b[:, 0, 0], a[:, 0, 0].copy() #swap

Time comparison:

%timeit a[:,0,0], b[:, 0, 0] = b[:, 0, 0], a[:, 0, 0].copy() #swapThe slowest run took 10.79 times longer than the fastest. This could mean that an intermediate result is being cached 1000000 loops, best of 3: 1.75 µs per loop%timeit t = np.copy(a[:,0,0]); a[:,0,0] = b[:,0,0]; b[:,0,0] = tThe slowest run took 10.88 times longer than the fastest. This could mean that an intermediate result is being cached 100000 loops, best of 3: 2.68 µs per loop


user4815162342's answer is indeed the "right" one. But if you're really after a one-liner, then consider this:

a[arange(2),0,0], b[arange(2), 0, 0] = b[arange(2), 0, 0], a[arange(2),0,0] #swap

This is however significantly less efficient:

In [12]: %timeit a[arange(2),0,0], b[arange(2), 0, 0] = b[arange(2), 0, 0], a[arange(2),0,0]10000 loops, best of 3: 32.2 µs per loopIn [13]: %timeit t = np.copy(a[:,0,0]); a[:,0,0] = b[:,0,0]; b[:,0,0] = tThe slowest run took 4.14 times longer than the fastest. This could mean that an intermediate result is being cached 100000 loops, best of 3: 13.3 µs per loop

(but notice the note about "the slowest run"... if you try to call %timeit with "-n 1 -r 1", you will see more comparable results - although with my solution still being ~50% slower - demonstrating that yes, caching is affecting the timings)