Iterating over a numpy array

前端 未结 3 1938
清酒与你
清酒与你 2020-11-28 01:47

Is there a less verbose alternative to this:

for x in xrange(array.shape[0]):
    for y in xrange(array.shape[1]):
        do_stuff(x, y)

I

3条回答
  •  不知归路
    2020-11-28 02:37

    If you only need the indices, you could try numpy.ndindex:

    >>> a = numpy.arange(9).reshape(3, 3)
    >>> [(x, y) for x, y in numpy.ndindex(a.shape)]
    [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
    

提交回复
热议问题