确定网格(阵列)上位置的有效方法

Nop*_*ope 2 python arrays performance list

我在python中表示一个带有2D列表的网格.我想在列表中选择一个点(x,y)并确定它的位置......右边缘,左上角,中间的某个位置......

目前我正在检查:

         # left column, not a corner
         if x == 0 and y != 0 and y != self.dim_y - 1:
             pass
         # right column, not a corner
         elif x == self.dim_x - 1 and y != 0 and y != self.dim_y - 1:
             pass
         # top row, not a corner
         elif y == 0 and x != 0 and x != self.dim_x - 1:
             pass
         # bottom row, not a corner
         elif y == self.dim_y - 1 and x != 0 and x != self.dim_x - 1:
             pass
         # top left corner
         elif x == 0 and y == 0:
             pass
         # top right corner
         elif x == self.dim_x - 1 and y == 0:
             pass
         # bottom left corner
         elif x == 0 and y == self.dim_y - 1:
             pass
         # bottom right corner
         elif x == self.dim_x - 1 and y == self.dim_y - 1:
             pass
         # somewhere in middle; not an edge
         else:
             pass
Run Code Online (Sandbox Code Playgroud)

在确定位置后,我有一些功能做某事

dim_x和dim_y是列表的维度.

没有那么多if-else语句,有更好的方法吗?有效的东西会很好,因为这部分逻辑被称为几百万次...它是模拟退火.

提前致谢.另外,什么是更好的措辞标题?

mob*_*mob 7

def location(x,y,dim_x,dim_y):
    index = 1*(y==0) + 2*(y==dim_y-1) + 3*(x==0) + 6*(x==dim_x-1)
    return ["interior","top","bottom","left","top-left",
            "bottom-left","right","top-right","bottom-right"][index]
Run Code Online (Sandbox Code Playgroud)