Iterating over a numpy array

前端 未结 3 1930
清酒与你
清酒与你 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:39

    see nditer

    import numpy as np
    Y = np.array([3,4,5,6])
    for y in np.nditer(Y, op_flags=['readwrite']):
        y += 3
    
    Y == np.array([6, 7, 8, 9])
    

    y = 3 would not work, use y *= 0 and y += 3 instead.

提交回复
热议问题