I first tried Swap two values in a numpy array.
But this seemingly simple problem leads to index errors or incorrect results, so I must be doing something wrong... but what?
import numpy as np
# Swap 1 and 3, leave the 0s alone!
i = np.array([1, 0, 1, 0, 0, 3, 0, 3])
# Swaps incorrectly
i[i==1], i[i==3] = 3, 1
# IndexError
i[i==1, i==3] = i[i==3, i==1]
# IndexError
i[[i==1, i==3]] = i[[i==3, i==1]]
# IndexError
ix1 = np.argwhere(i==1)
ix3 = np.argwhere(i==3)
i[[ix1, ix3]] = i[[ix3, ix1]]
# Swaps incorrectly
i[np.argwhere(i==1)], i[np.argwhere(i==3)] = 3, 1