Numpy combines two 2d markets

I am working on a puzzle in python.

What I'm trying to do is cover a piece for display.

For example:

 gameMap = np.array([[1 0 0]
                     [0 1 0]
                     [0 1 1]])
 piece = np.array([[0, 1],
                   [1, 1]])

How can I put a piece on a card to get a result like

[[1 1 0]
 [1 2 0]
 [0 1 1]]

Or

[[1 0 0]
 [0 1 1]
 [0 2 2]]

Thanks in advance.

+4
source share
2 answers

One way to “add” your fragment to the map is to use slicing. The key selects a GameMap slice that has the same shape as the slice.

gameMap[0:2, 0:2] += piece

Output:

[[1 1 0]
 [1 2 0]
 [0 1 1]]

OR

gameMap[1:3, 1:3] += piece

Output:

[[1 0 0]
 [0 1 1]
 [0 2 2]] 
+3
source

Another way to do this is to add a second array at the same size as your first array.

print gameMap + np.pad(piece, ((0,1), (0,1)), 'constant')
print gameMap + np.pad(piece, ((1,0), (1,0)), 'constant')

, , ( ) , , .

+1

Source: https://habr.com/ru/post/1627537/


All Articles