[Numpy-discussion] ndrange, like range but multidimensiontal

Allan Haldane allanhaldane at gmail.com
Sun Oct 7 17:20:11 EDT 2018

On 10/07/2018 10:32 AM, Mark Harfouche wrote:
> With `np.ndrange` it can look something like this:
> ```
> c = np.empty((4, 4), dtype=object)
> # ... compute on c
> for i in np.ndrange(c.shape)[:, 1:-1]:
>      c[i] # = some operation on c[i] that depends on the index i
> ```
> very pythonic, very familiar to numpy users

So if I understand, this does the same as `np.ndindex` but allows 
numpy-like slicing of the returned generator object, as requested in #6393.

I don't like the duplication in functionality between ndindex and 
ndrange here. Better rather to add the slicing functionality to ndindex, 
  than create a whole new nearly-identical function. np.ndindex is 
already a somewhat obscure and discouraged method since it is usually 
better to find a vectorized numpy operation instead of a for loop, and I 
don't like adding more obscure functions.

But as an improvement to np.ndindex, I think adding this functionality 
seems good if it can be nicely implemented. Maybe there is a way to use 
the same optimization tricks as in the current implementation of ndindex 
but allow different stop/step? A simple wrapper of ndindex?


More information about the NumPy-Discussion mailing list