📅  最后修改于: 2022-03-11 14:47:00.842000             🧑  作者: Mango
mat = "NxN matrix"
a, b = (2, 3) # the index of the element
neighbors = [mat[i][j] for i in range(a-1, a+2) for j in range(b-1, b+2) if i > -1 and j > -1 and j < len(mat[0]) and i < len(mat)]