Pythonic way to iterate through multidimensional space?

Peter Otten __peter__ at web.de
Wed Aug 6 08:39:36 EDT 2014


Gayathri J wrote:

> Dear Peter
> 
> Yes the f[t] or f[:,:,:] might give  a marginal increase, 

The speedup compared itertools.product() is significant:

$ python -m timeit -s 'from itertools import product; from numpy.random 
import rand; N = 100; a = rand(N, N, N); r = range(N)' 'for x in product(r, 
repeat=3): a[x] = 0.0'
10 loops, best of 3: 290 msec per loop

$ python -m timeit -s 'from itertools import product; from numpy.random 
import rand; N = 100; a = rand(N, N, N); r = range(N)' 'a[:,:,:] = 0.0'
100 loops, best of 3: 3.58 msec per loop

But normally you'd just make a new array with numpy.zeros().

> but then i need
> to do further operations  using the indices, in which case this wouldnt
> help

Which is expected and also the crux of such micro-benchmarks. They distract 
from big picture.




More information about the Python-list mailing list