This seems like a trick:
def addscores(mat): for y in range(len(mat)): for x in range(len(mat[y])): if mat[y][x] == 'b': mat = pad(mat, x, y, '1') return mat def pad(mat, x, y, n): for i, (x,y) in enumerate(itertools.product(range(x-1, x+2), range(y-1, y+2))): if i != 4: # the coordinate at index 4 is where the bomb is if 0<=y<len(mat) and 0<=x<len(mat[y]): mat[y][x] = n return mat
Testing:
In [127]: mat Out[127]: [['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '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', '1', '0', '1']] In [129]: addscores(mat) Out[129]: [['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '0', '1', '1'], ['0', '0', '0', '0', '0', '0', '0', '0', '1', 'b'], ['0', '0', '0', '0', '0', '0', '0', '0', '1', '1']]