Iterating over arbitrary dimension of numpy.array

前端 未结 5 1592
生来不讨喜
生来不讨喜 2020-12-01 08:55

Is there function to get an iterator over an arbitrary dimension of a numpy array?

Iterating over the first dimension is easy...

In [63]: c = numpy.a         


        
5条回答
  •  一向
    一向 (楼主)
    2020-12-01 09:38

    I guess there is no function. When I wrote my function, I ended up taking the iteration EOL also suggested. For future readers, here it is:

    def iterdim(a, axis=0) :
      a = numpy.asarray(a);
      leading_indices = (slice(None),)*axis
      for i in xrange(a.shape[axis]) :
        yield a[leading_indices+(i,)]
    

提交回复
热议问题