Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法防止索引换行?_Python_Matrix - Fatal编程技术网

Python 有没有办法防止索引换行?

Python 有没有办法防止索引换行?,python,matrix,Python,Matrix,我有以下资料: # Possible neighbors as tuples UP = (-1,0) DOWN = (1,0) LEFT = (0,-1) RIGHT = (0,1) POTENTIAL_NEIGHBORS = (UP, DOWN, LEFT, RIGHT) def LocateNeighbors(self): print '\nWorking on: {}:{}'.format(self.index,self.value) for n in P

我有以下资料:

# Possible neighbors as tuples
UP = (-1,0)
DOWN = (1,0)
LEFT = (0,-1)
RIGHT = (0,1)
POTENTIAL_NEIGHBORS = (UP, DOWN, LEFT, RIGHT)

def LocateNeighbors(self):
        print '\nWorking on: {}:{}'.format(self.index,self.value)
        for n in POTENTIAL_NEIGHBORS:
            try:
                if theBoard[self.index[0]+n[0]][self.index[1]+n[1]]:
                    print "Adding neighbor: " + theBoard[self.index[0]+n[0]][self.index[1]+n[1]]
                    self.neighbors.append(n)
            except IndexError:
                print 'failed on: {}'.format(n)
。。其中self是游戏板中的一个单元,例如:

      A      B      C      D      E      F
1 | {66} | {76} | {28} | {66} | {11} | {09}
-------------------------------------------
2 | {31} | {39} | {50} | {08} | {33} | {14}
-------------------------------------------
3 | {80} | {76} | {39} | {59} | {02} | {48}
-------------------------------------------
4 | {50} | {73} | {43} | {03} | {13} | {03}
-------------------------------------------
5 | {99} | {45} | {72} | {87} | {49} | {04}
-------------------------------------------
6 | {80} | {63} | {92} | {28} | {61} | {53}
-------------------------------------------
但是,这是我检查的第一个单元格0,0的输出:


本质上,如果索引是我认为无效的,它就被打包到列表的末尾。有没有办法防止或至少检测这种行为?或者,也许是一种更优雅的方法?

您可以使用范围检查器功能:

def check(n):
    if n<0:
        raise IndexError(n)
    return n

def LocateNeighbors(self):
    print '\nWorking on: {}:{}'.format(self.index,self.value)
    for n in POTENTIAL_NEIGHBORS:
        try:
            if theBoard[check(self.index[0]+n[0])][check(self.index[1]+n[1])]:
                print "Adding neighbor: " + theBoard[self.index[0]+n[0]][self.index[1]+n[1]]
                self.neighbors.append(n)
        except IndexError:
            print 'failed on: {}'.format(n)

您可以使用范围检查器功能:

def check(n):
    if n<0:
        raise IndexError(n)
    return n

def LocateNeighbors(self):
    print '\nWorking on: {}:{}'.format(self.index,self.value)
    for n in POTENTIAL_NEIGHBORS:
        try:
            if theBoard[check(self.index[0]+n[0])][check(self.index[1]+n[1])]:
                print "Adding neighbor: " + theBoard[self.index[0]+n[0]][self.index[1]+n[1]]
                self.neighbors.append(n)
        except IndexError:
            print 'failed on: {}'.format(n)

沿以下行添加一个复选框:

if (0 <= (self.index[0] + n[0]) < num_cols) and 
   (0 <= (self.index[1] + n[1]) < num_rows):
    # Do stuff here
这样,当您位于板的相对边缘时,也应该希望避免索引器

编辑:


我认为丹尼尔的回答更像是蟒蛇。它遵循以下原则。

添加一个沿着以下行的检查:

if (0 <= (self.index[0] + n[0]) < num_cols) and 
   (0 <= (self.index[1] + n[1]) < num_rows):
    # Do stuff here
这样,当您位于板的相对边缘时,也应该希望避免索引器

编辑:

我认为丹尼尔的回答更像是蟒蛇。它遵循这个原则