Python2D列表值检查

Python2D列表值检查,python,list,2d,Python,List,2d,以下脚本使用python生成二维列表: matrix = [[0 for row in range (5)] for col in range (5)] i = 2 matrix[i][i] = 1 matrix[i+1][i] = 1 matrix[i][i+1] = 1 matrix[i+1][i+1] = 1 for row in matrix: for item in row: print(item,end=" ") print()

以下脚本使用python生成二维列表:

matrix = [[0 for row in range (5)] for col in range (5)]


i = 2
matrix[i][i] = 1
matrix[i+1][i] = 1
matrix[i][i+1] = 1
matrix[i+1][i+1] = 1


for row in matrix:
    for item in row:
        print(item,end="   ")

    print()
    print()
生成的二维列表如下所示:

0   0   0   0   0   

0   0   0   0   0   

0   0   1   1   0   

0   0   1   1   0   

0   0   0   0   0 

我怎样才能找到一个和上面显示的数字相同(数字必须是1)的正方形?具有相同数字的正方形必须为2x2

您可以对行和单元格进行迭代,对于每个左上角,检查单元格的正方形是否都等于1,如果等于1,则返回True:

def has_square(seq):
    for row, line in enumerate(seq[:-1]):
        for col, cell in enumerate(line[:-1]):
            if cell == 1:
                if seq[row][col+1] == 1 and seq[row+1][col] == 1 and seq[row+1][col+1] == 1:
                    return True
    return False
Has square==True: Has square==False:
它相当粗糙,不能很好地推广,但它做了您想要的:确定序列是否有四个1的平方。

我们可以迭代矩阵中的每个元素,直到找到1。如果我们遇到1,那么我们可以检查它的邻域元素是否包含1,因为如果我们没有遇到1,那么它与它的邻域形成的矩阵不符合我们的兴趣

下面是执行此操作的代码:

def check_matrix(i,j):
    if matrix[i][i+1] ==1 and matrix[i+1][j]==1 and matrix[i+1][j+1]==1:
        return True
    else:
        return False

for i in range(5):
    for j in range(5):
        if matrix[i][j] == 1:
            check = check_matrix(i,j)
            if check == True:
                print('found at index',i,j)

该程序将检查矩阵中的1,如果遇到1,则检查其相邻元素中的1,如果全部为1,则返回True,否则返回False。

如何使其易于自定义

width = 5 # Width of grid
height = 5
square_w = 2 # Width of square you want found
square_h = 2
square_coords = []
avoid = [0] # This number doesn't count as a square
grid_list = [0, 0, 0, 0, 0,
             0, 0, 0, 0, 0,
             0, 0, 1, 1, 0,
             0, 0, 1, 1, 0,
             0, 0, 0, 0, 0] # Grid

# ONLY EDIT ABOVE /\ /\ /\

conv_list = []
for rep_h in range(height): # Convert grid
    conv_list += [[0] * width]
    for rep_w in range(width):
        conv_list[rep_h][rep_w] = grid_list[rep_h*width+rep_w]

for rep_h in range(height-square_h+1): # Find squares
    for rep_w in range(width-square_w+1):
        square_vis = True
        if not conv_list[rep_h][rep_w] in avoid:
            square_val = conv_list[rep_h][rep_w]
            for h in range(square_h):
                for w in range(square_w):
                    if conv_list[rep_h+h][rep_w+w] != square_val:
                        square_vis = False
            if square_vis:
                square_coords += [[rep_h, rep_w]]

print(square_coords) # Print result (Starting coordinates, AKA top left of box found)

你的尝试是什么?在哪里失败了?请提供代码,阅读并添加生成列表的代码。我被卡住了,我想不出任何办法来实际检查这些值是否相同。您是否正在尝试查找数组的所有索引
(I,j)
,它们满足某些条件,如
矩阵[I][j]==1
?不,我只需要找到一个包含1的2x2正方形。如果发现这样的方块,程序将终止。非常感谢。拯救了这一天。工作完美。
def check_matrix(i,j):
    if matrix[i][i+1] ==1 and matrix[i+1][j]==1 and matrix[i+1][j+1]==1:
        return True
    else:
        return False

for i in range(5):
    for j in range(5):
        if matrix[i][j] == 1:
            check = check_matrix(i,j)
            if check == True:
                print('found at index',i,j)
width = 5 # Width of grid
height = 5
square_w = 2 # Width of square you want found
square_h = 2
square_coords = []
avoid = [0] # This number doesn't count as a square
grid_list = [0, 0, 0, 0, 0,
             0, 0, 0, 0, 0,
             0, 0, 1, 1, 0,
             0, 0, 1, 1, 0,
             0, 0, 0, 0, 0] # Grid

# ONLY EDIT ABOVE /\ /\ /\

conv_list = []
for rep_h in range(height): # Convert grid
    conv_list += [[0] * width]
    for rep_w in range(width):
        conv_list[rep_h][rep_w] = grid_list[rep_h*width+rep_w]

for rep_h in range(height-square_h+1): # Find squares
    for rep_w in range(width-square_w+1):
        square_vis = True
        if not conv_list[rep_h][rep_w] in avoid:
            square_val = conv_list[rep_h][rep_w]
            for h in range(square_h):
                for w in range(square_w):
                    if conv_list[rep_h+h][rep_w+w] != square_val:
                        square_vis = False
            if square_vis:
                square_coords += [[rep_h, rep_w]]

print(square_coords) # Print result (Starting coordinates, AKA top left of box found)