numpy数组indexerror问题

时间:2018-12-11 14:12:48

标签: python arrays indexoutofboundsexception

arr = np.arange(1,21).reshape(4,5)
print(arr)

def find_depot_placement(grid):
    for x in range(grid.shape[0]):
        for y in range(grid.shape[1]):
            start = grid[x][y]
            down = grid[x+1][y]
            bot_right = grid[x+1][y+1]
            right = grid[x][y+1]
            check_map_bounds(grid, start)
            check_map_bounds(grid, down)
            check_map_bounds(grid, bot_right)
            check_map_bounds(grid, right)


def check_map_bounds(grid, position):
    if position in grid:
        print("position", position)
    else:
        pass

我知道出了什么问题,循环最终在已建立数组的索引之外寻找元素。如何将其保持在数组范围内?我希望它在到达边界的尽头时从下一组数字开始

[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]
 [16 17 18 19 20]]
position 1
position 6
position 7
position 2
position 2
position 7
position 8
position 3
position 3
position 8
position 9
position 4
position 4
position 9
position 10
position 5
IndexError: index 5 is out of bounds for axis 0 with size 5

0 个答案:

没有答案