在2D Numpy数组上切片,索引和迭代

时间:2018-10-01 19:55:29

标签: python arrays numpy

我在我的大学参加了Python的初学者课程。我们有一个编程任务,我一直坚持。

我们得到一个分配,以查找地图中两个点之间的路线,该地图是2D numpy数组。 首要任务之一是将由自由路(1)和建筑物(0)组成的数组转换为将建筑物周围的所有位置(左,右,下方,上方)转换为停车位(-1)的数组。

我首先编写了一个函数来生成正确大小的2D numpy数组:

def get_map():

    map = np.random.randint(2, size=(12, 10))

    return map

现在,我想编写另一个函数,该函数将地图作为参数,并返回将停车位从1转换为-1的地图。

def adjusted_map(map): 

    map_adjusted = 

    return map_adjusted

我主要停留在0上下的元素上。我可以左右,因为这与一维数组或普通列表,字符串等没有什么不同。 很抱歉,如果这是一个愚蠢的问题,但我查阅了有关numpy数组的索引,切片和迭代的numpy文档,但找不到解决方案。

2 个答案:

答案 0 :(得分:2)

这是使用标准numpy技术的一种简单方法:

1)制作一张地图,其中包含3x3块,道路占80%

>>> map_ = np.kron(np.random.random((6, 5)) < 0.8, np.ones((3, 3), int))
>>> map_
array([[1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0]])

2)反转地图并将其零填充

>>> helper = np.pad(1-map_, ((1, 1), (1, 1)), 'constant')
>>> helper
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

3)切出逆映射的偏移版本(上,下,左,右),使用(按位)or查找建筑物的所有邻居,使用and仅保留那些是路

>>> parking = map_ & (helper[2:, 1:-1] | helper[:-2, 1:-1] | helper[1:-1, 2:] | helper[1:-1, :-2])
>>> parking
array([[0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1],
       [0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0],
       [0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0],
       [0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0]])

4)在地图上标记停车位

>>> result = map_ - 2*parking
>>> result
array([[ 1,  1, -1,  0,  0,  0,  0,  0,  0, -1,  1,  1,  1,  1,  1],
       [ 1,  1, -1,  0,  0,  0,  0,  0,  0, -1,  1,  1,  1,  1,  1],
       [ 1,  1, -1,  0,  0,  0,  0,  0,  0, -1,  1,  1,  1,  1,  1],
       [ 1,  1, -1,  0,  0,  0, -1, -1, -1,  1,  1,  1,  1,  1,  1],
       [ 1,  1, -1,  0,  0,  0, -1,  1,  1,  1,  1,  1,  1,  1,  1],
       [-1, -1, -1,  0,  0,  0, -1, -1, -1,  1,  1,  1,  1,  1,  1],
       [ 0,  0,  0, -1, -1, -1,  0,  0,  0, -1,  1,  1,  1,  1,  1],
       [ 0,  0,  0, -1,  1, -1,  0,  0,  0, -1,  1,  1,  1,  1,  1],
       [ 0,  0,  0, -1,  1, -1,  0,  0,  0, -1,  1,  1,  1,  1,  1],
       [ 0,  0,  0, -1,  1,  1, -1, -1, -1,  1,  1,  1,  1,  1,  1],
       [ 0,  0,  0, -1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1],
       [ 0,  0,  0, -1,  1,  1, -1, -1, -1,  1,  1,  1,  1,  1,  1],
       [-1, -1, -1,  1,  1, -1,  0,  0,  0, -1,  1,  1,  1,  1,  1],
       [ 1,  1,  1,  1,  1, -1,  0,  0,  0, -1,  1,  1,  1,  1,  1],
       [-1, -1, -1,  1,  1, -1,  0,  0,  0, -1,  1,  1, -1, -1, -1],
       [ 0,  0,  0, -1,  1, -1,  0,  0,  0, -1,  1, -1,  0,  0,  0],
       [ 0,  0,  0, -1,  1, -1,  0,  0,  0, -1,  1, -1,  0,  0,  0],
       [ 0,  0,  0, -1,  1, -1,  0,  0,  0, -1,  1, -1,  0,  0,  0]])

5)奖金:美化

>>> symbols = np.array(('x', '.', 'P'))
>>> rowtype = f'U{map_.shape[1]}'
>>> rowtype
'U15'
>>> print('\n'.join(symbols[map_].view(rowtype).ravel()))
...xxxxxx......
...xxxxxx......
...xxxxxx......
...xxx.........
...xxx.........
...xxx.........
xxx...xxx......
xxx...xxx......
xxx...xxx......
xxx............
xxx............
xxx............
......xxx......
......xxx......
......xxx......
xxx...xxx...xxx
xxx...xxx...xxx
xxx...xxx...xxx
>>> print('\n'.join(symbols[result].view(rowtype).ravel()))
..PxxxxxxP.....
..PxxxxxxP.....
..PxxxxxxP.....
..PxxxPPP......
..PxxxP........
PPPxxxPPP......
xxxPPPxxxP.....
xxxP.PxxxP.....
xxxP.PxxxP.....
xxxP..PPP......
xxxP...........
xxxP..PPP......
PPP..PxxxP.....
.....PxxxP.....
PPP..PxxxP..PPP
xxxP.PxxxP.Pxxx
xxxP.PxxxP.Pxxx
xxxP.PxxxP.Pxxx

答案 1 :(得分:0)

所以您需要这样的东西:

import numpy as np

mapper = np.random.randint(2, size=(12, 10))
buildings = np.nonzero(mapper)

nonzero_row = buildings[0]
nonzero_col = buildings[1]

for row, col in zip(nonzero_row, nonzero_col):
    if row > 0:
        mapper[row-1, col] = -1
    if col > 0:
        mapper[row, col-1] = -1
    if row < mapper.shape[0]:
        mapper[row+1,col] = -1
    if col < mapper.shape[1]:
        mapper[row,col+1] = -1