I am trying to get some array indices with python. At the moment, the code looks very cumbersome and I was wondering if I am doing it inefficiently or in unpythonic style. So, I have an n-dimensional array and I am trying to generate some indexes as follows. Here is an isolated code sample that demonstrates what I am trying to do. I am demonstrating this in a simple 2D code segment but the array can be arbitrary dimension.
import numpy as np
a = np.random.rand(5, 5)
shape = a.shape
for i in range(len(shape)):
shape_temp = np.zeros(len(shape), dtype=np.int)
shape_temp[i] = 1
p = np.meshgrid(*[np.arange (shape_temp[l], shape[l]) for l in range(len(shape))])
# Do something with p
I was wondering if there was a more elegant and hopefully efficient way to generate these indices? In particular the use of this shape_temp variable looks ugly especially how it is created every time in the loop.