Python 正在检查数组中的值

Python 正在检查数组中的值,python,debugging,indexing,Python,Debugging,Indexing,我有一个数组,如下所示 testgrid = [ [9, 1, 2, 4, 3, 8, 7, 5, 6], [9, 1, 2, 4, 3, 8, 7, 5, 6], [9, 1, 2, 4, 3, 8, 7, 5, 6], [9, 1, 2, 4, 3, 8, 7, 5, 6], [9, 1, 2, 4, 3, 8, 7, 5, 6], [9, 1, 2, 4, 3, 8, 7, 5, 6], [9, 1, 2, 4, 3,

我有一个数组,如下所示

testgrid = [
     [9, 1, 2, 4, 3, 8, 7, 5, 6],
     [9, 1, 2, 4, 3, 8, 7, 5, 6],
     [9, 1, 2, 4, 3, 8, 7, 5, 6],
     [9, 1, 2, 4, 3, 8, 7, 5, 6],
     [9, 1, 2, 4, 3, 8, 7, 5, 6],
     [9, 1, 2, 4, 3, 8, 7, 5, 6],
     [9, 1, 2, 4, 3, 8, 7, 5, 6],
     [9, 1, 2, 4, 3, 8, 7, 5, 6],
     [9, 1, 2, 4, 3, 8, 7, 5, 6]]
在将testgrid传递到以下函数时,它应该返回当前返回的不包含零值(0,0)的所有索引。我希望所有索引都会在这个网格上返回。它似乎在检查索引,而不是该索引中存储的值。我是个笨蛋,所以我可能错过了一些明显的东西

def not_empty_location(grid):

    # checks if current location is empty and assign location not empty
    for i in range(9):
        for j in range(9):
            if grid[i][j] != 0:
                return (i, j)
    return None

使用
yield
代替
return

def非空位置(网格):
#检查当前位置是否为空,分配位置是否为非空
对于范围(9)内的i:
对于范围(9)内的j:
如果网格[i][j]!=0:
收益率(i,j)
一无所获
这将以
生成器
的形式返回所有值,然后可以简单地将其转换为
列表

输出:

[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8)]

使用
yield
代替
return

def非空位置(网格):
#检查当前位置是否为空,分配位置是否为非空
对于范围(9)内的i:
对于范围(9)内的j:
如果网格[i][j]!=0:
收益率(i,j)
一无所获
这将以
生成器
的形式返回所有值,然后可以简单地将其转换为
列表

输出:

[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8)]

尝试将索引元组存储在列表中并返回

    def not_empty_location(grid):
    result = [] # list to store the index
    # checks if current location is empty and assign location not empty

    for i in range(9):
        for j in range(9):
            if grid[i][j] != 0:
                result.append( (i, j) )   # In your code you are returning at your very first success of getting != 0 
    return result

尝试将索引元组存储在列表中并返回

    def not_empty_location(grid):
    result = [] # list to store the index
    # checks if current location is empty and assign location not empty

    for i in range(9):
        for j in range(9):
            if grid[i][j] != 0:
                result.append( (i, j) )   # In your code you are returning at your very first success of getting != 0 
    return result

您的循环在第一次迭代时返回。您必须将结果存储到一个数组中,并在for循环运行到完整性之后返回结果,程序当前称应该返回第一个非零元素的坐标。您必须将结果存储到一个数组中,并在for循环运行到完整性之后返回结果,程序当前称应该返回第一个非零元素的坐标。