I want to swap rows in a two-dimensional array. Using advanced slicing is quite convenient for this purpose:
In [50]: l
Out[50]: [[0, 1], [2, 3], [4, 5], [6, 7]]
In [51]: l[::2], l[1::2] = l[1::2], l[::2]
In [52]: l
Out[52]: [[2, 3], [0, 1], [6, 7], [4, 5]]
However, this does not work if I convert the list into Numpy array:
In [60]: arr
Out[60]:
array([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
In [61]: arr[::2], arr[1::2] = arr[1::2], arr[::2]
In [62]: arr
Out[62]:
array([[2, 3],
[2, 3],
[6, 7],
[6, 7]])
Why does this method of swapping not work for Numpy arrays?