0

I have been using the code

BOARD = [[0 for i in range(GRIDWIDTH)] for j in range(GRIDWIDTH)]

to create a 2 dimensional 'grid' for part of my pygame project.

I would like to know how I can fill in the edges of the grid with a different number, so I have all zeros in the centre of the grid, surrounded by ones on the edges.

Thanks!

4

2 に答える 2

1
BOARD = [[1] * GRIDWIDTH] + [
     [1] + [0]*(GRIDWIDTH-2) + [1] for j in range(GRIDWIDTH-2)
   ] + [[1] * GRIDWIDTH]
于 2013-02-23T13:20:30.223 に答える
1

ここでnumpyを使用することをお勧めします

>>> import numpy as np
>>> grid = np.zeros(shape=(10,10))
>>> grid[[0,-1],:] , grid[:,[0,-1]] = 1, 1
>>> grid
array([[ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.]])
>>> 
于 2013-02-23T13:35:42.030 に答える