I'm not a big fan of list comprehensions in general. I prefer the clearer loop format. You can compress the code significantly by using ranges. To check all the cells around a given cell you just need to loop over range(row-1, row+2) and range(col-1, col_2) while adding tests to ensure you stay within the defined grid as in
for r in range(max(row-1,0), min(row+2,numrows))
The rest of this should be fairly obvious.
<pre>
def printGrid(grid)
for row in grid:
out = ''
for col in row:
out += f'{col:^3}'
print(out)
def numAdjacent(grid: list, row: int, col: int) -> int
numrows,numcols = len(grid), len(grid[0])
nummines = 0
for r in range(max(row-1,0), min(row+2,numrows)):
for c in range(max(col-1,0), min(col+2,numcols)):
nummines += grid[r][c] == '#'
return nummines
def mineSweeper(grid: list) -> list
numrows = len(grid)
numcols = len(grid[0])
newgrid = grid
for row in range(0, numrows):
for col in range(0, numcols):
if newgrid[row][col] == '-':
newgrid[row][col] = numAdjacent(grid,row,col)
return newgrid
grid = [
["-", "-", "-", "#", "#"],
["-", "#", "-", "-", "-"],
["-", "-", "#", "-", "-"],
["-", "#", "#", "-", "-"],
["-", "-", "-", "-", "-"],
]
printGrid(grid)
print('')
printGrid(mineSweeper(grid))