<div dir="ltr">I have a function that operates over a 1D array, to return an array of a similar size.  To use it in a 2D fashion I would have to do something like the following:<div><br></div><div>for row in range(np.size(arr, 0):</div><div>    arr_out[row] = func(arr[row])</div><div>for col in range(np.size(arr, 1):</div><div>    arr_out[:, col] = func(arr[:, col])</div><div><br></div><div><div>I would like to generalise this to N dimensions. Does anyone have any suggestions of how to achieve this?  Presumably what I need to do is build an iterator, and then remove an axis:</div><div><br></div><div># arr.shape=(2, 3, 4)</div><div>it = np.nditer(arr, flags=['multi_index'])</div><div>it.remove_axis(2)</div><div>while not it.finished:</div><div>    arr_out[it.multi_index] = func(arr[it.multi_index])</div><div>    it.iternext()</div><div><br></div><div>If I have an array with shape (2, 3, 4) this would allow me to iterate over the 6 1D arrays that are 4 elements long.  However, how do I then construct the iterator for the preceding axes?</div></div></div>