-5
A
答えて
0
# 2dMatrix: Your matrix
# x: x-coordinate of the element for which you're searching for neighbors
# y: y-coordinate of the element for which you're searching for neighbors
def nearest_elements_2dMatrix(2dMatrix, x,y):
upleft = 2dMatrix[x-1][y-1] if (x-1 >= 0 and y-1>=0) else None
downleft = 2dMatrix[x-1][y+1] if (x-1 >= 0 and y+1 < len(2dMatrix)) else None
left = 2dMatrix[x-1][y] if x-1 >= 0 else None
upright = 2dMatrix[x+1][y-1] if (x+1 < len(2dMatrix[0]) and y-1>=0) else None
right= 2dMatrix[x+1][y] if x+1 < len(2dMatrix[0]) else None
downright = 2dMatrix[x+1][y+1] if (x+1 < len(2dMatrix[0]) and y+1 < len(2dMatrix)) else None
above= 2dMatrix[x][y-1] if if y-1 >= 0 else None
below= 2dMatrix[x][y+1] if if y+1 < len(2dMatrix) else None
return [upleft, downleft, left, upright, right, downright, above, below]
+0
あなたは対角線を忘れてしまい、入力が座標ではなく値であるという事実を忘れてしまいました。 –
+0
対角線を反映するように編集しましたが、入力が座標でない場合、一意でない値が含まれています – knowa42
です... –
私の英語は((申し訳ありません –
(悪いです次に、[ru.SO]を試してください。 –