Iterating over arbitrary dimension of numpy.array

前端 未结 5 1600
生来不讨喜
生来不讨喜 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:20

    I'd use the following:

    c = numpy.arange(2 * 3 * 4)
    c.shape = (2, 3, 4)
    
    for r in numpy.rollaxis(c, 2):
        print(r)
    

    The function rollaxis creates a new view on the array. In this case it's moving axis 2 to the front, equivalent to the operation c.transpose(2, 0, 1).

提交回复
热议问题