b = a[(slice(None),) * k + (i,)]
Create an indexing tuple manually.
As stated in the Python link , the form expression
a[:, :, :, :, :, i]
converted to
a[(slice(None), slice(None), slice(None), slice(None), slice(None), i)]
We can achieve the same effect by creating this tuple directly, instead of using notation for slicing. (There's a small caveat that building a tuple directly creates a[(i,)]
instead a[i]
for k=0
, but NumPy treats them the same way for scalar i
.)