Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.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 如何删除矩阵单元格';s与其值相同的邻居_Python_Python 3.x_Matrix - Fatal编程技术网

Python 如何删除矩阵单元格';s与其值相同的邻居

Python 如何删除矩阵单元格';s与其值相同的邻居,python,python-3.x,matrix,Python,Python 3.x,Matrix,我有一个如下所示的矩阵(取自带有参数的txt文件),每个单元格都有邻居。拾取一个单元格后,该单元格和包含相同编号的所有相邻单元格将消失 1 0 4 7 6 8 0 5 4 4 5 5 2 1 4 4 4 6 4 1 3 7 4 4 我尝试过使用递归来实现这一点。我将函数分为四个部分,分别是up()、down()、left()和right()。但我收到了一条错误消息:RecursionError:比较中超过了最大递归深度 cmd=input("Row,column:") cmdlist=comm

我有一个如下所示的矩阵(取自带有参数的txt文件),每个单元格都有邻居。拾取一个单元格后,该单元格和包含相同编号的所有相邻单元格将消失

1 0 4 7 6 8
0 5 4 4 5 5
2 1 4 4 4 6
4 1 3 7 4 4
我尝试过使用递归来实现这一点。我将函数分为四个部分,分别是
up()
down()
left()
right()
。但我收到了一条错误消息:
RecursionError:比较中超过了最大递归深度

cmd=input("Row,column:")
cmdlist=command.split(",")
row,column=int(cmdlist[0]),int(cmdlist[1])
num=lines[row-1][column-1]
def up(x,y):
    if lines[x-2][y-1]==num and x>1:
        left(x,y)
        right(x,y)
        lines[x-2][y-1]=None
def left(x,y):
    if lines[x-1][y-2]==num and y>1:     
        up(x,y)
        down(x,y)
        lines[x-1][y-2]=None      
def right(x,y):
    if lines[x-1][y]==num and y<len(lines[row-1]):      
        up(x,y)
        down(x,y)
        lines[x-1][y]=None    
def down(x,y):
    if lines[x][y-1]==num and x<len(lines):    
        left(x,y)
        right(x,y)
        lines[x][y-1]=None               
up(row,column)
down(row,column)
for i in lines:
    print(str(i).strip("[]").replace(",","").replace("None"," "))

我不需要固定的代码,只要主要的想法就足够了。非常感谢。

也许你应该换个新的

def right(x,y):
    if lines[x-1][y]==num and y<len(lines[row-1]):      
        up(x,y)
        down(x,y)
        lines[x-1][y]=None 
def右(x,y):

如果第[x-1][y]==num行和第y行的递归未终止,则会发生递归错误


您可以使用
集合
的索引来解决此问题,而无需递归:

  • 将包含查找的
    num
    ber的所有索引搜索到
    all\u num\u idx
  • 将您当前所在的索引(您的输入)添加到集合
    tbd
    (待删除)
  • tbd
    上循环,并将
    all_num_idx
    中仅在行或列中-1/+1处不同的所有索引添加到集合中已存在的任何索引
  • 直到
    tbd
    不再增长
  • tbd
    删除所有索引:

    t = """4 0 4 7 6 8
    0 5 4 4 5 5
    2 1 4 4 4 6
    4 1 3 7 4 4"""
    
    data = [k.strip().split() for k in t.splitlines()]
    
    row,column=map(int,input("Row,column:").strip().split(";"))
    num = data[row][column]
    
    len_r =len(data)
    len_c = len(data[0])
    
    all_num_idx = set((r,c) for r in range(len_r) for c in range(len_c) if data[r][c]==num)
    
    tbd = set( [ (row,column)] ) # inital field
    tbd_size = 0                 # different size to enter while
    done = set()                 # we processed those already
    while len(tbd) != tbd_size:  # loop while growing
        tbd_size=len(tbd)
        for t in tbd:
            if t in done:
                continue
            # only 4-piece neighbourhood +1 or -1 in one direction
            poss_neighbours = set( [(t[0]+1,t[1]), (t[0],t[1]+1),
                                    (t[0]-1,t[1]), (t[0],t[1]-1)] )
            # 8-way neighbourhood with diagonals
            # poss_neighbours = set((t[0]+a,t[1]+b) for a in range(-1,2) for b in range(-1,2))
            tbd = tbd.union( poss_neighbours & all_num_idx) 
            # reduce all_num_idx by all those that we already addded 
            all_num_idx -= tbd
            done.add(t) 
    
    # delete the indexes we collected
    for r,c in tbd:
        data[r][c]=None
    
    # output
    for line in data:
        print(*(c or " " for c in line) , sep=" ")
    
    输出:

    Row,column: 3,4
    
    4 0   7 6 8
    0 5     5 5
    2 1       6
    4 1 3 7    
    


    这是“泛洪填充算法”的一个变体,仅泛洪特定值的单元格。请参见

    这样打印应该更容易:
    print(*(e或“”表示i中的e))
    -更少的字符串替换和创建我认为
    max recursions错误
    是因为在调用up和down函数之后而不是之前修改了矩阵元素的值。出现错误的原因是
    up
    调用
    right
    ,调用
    up
    ,以此类推,不改变参数或其他任何可能打破链条的东西。我的答案使用了8向邻域-我对其进行了注释,并将其替换为4向邻域,就像您的代码使用的一样-参见编辑的答案
    t = """4 0 4 7 6 8
    0 5 4 4 5 5
    2 1 4 4 4 6
    4 1 3 7 4 4"""
    
    data = [k.strip().split() for k in t.splitlines()]
    
    row,column=map(int,input("Row,column:").strip().split(";"))
    num = data[row][column]
    
    len_r =len(data)
    len_c = len(data[0])
    
    all_num_idx = set((r,c) for r in range(len_r) for c in range(len_c) if data[r][c]==num)
    
    tbd = set( [ (row,column)] ) # inital field
    tbd_size = 0                 # different size to enter while
    done = set()                 # we processed those already
    while len(tbd) != tbd_size:  # loop while growing
        tbd_size=len(tbd)
        for t in tbd:
            if t in done:
                continue
            # only 4-piece neighbourhood +1 or -1 in one direction
            poss_neighbours = set( [(t[0]+1,t[1]), (t[0],t[1]+1),
                                    (t[0]-1,t[1]), (t[0],t[1]-1)] )
            # 8-way neighbourhood with diagonals
            # poss_neighbours = set((t[0]+a,t[1]+b) for a in range(-1,2) for b in range(-1,2))
            tbd = tbd.union( poss_neighbours & all_num_idx) 
            # reduce all_num_idx by all those that we already addded 
            all_num_idx -= tbd
            done.add(t) 
    
    # delete the indexes we collected
    for r,c in tbd:
        data[r][c]=None
    
    # output
    for line in data:
        print(*(c or " " for c in line) , sep=" ")
    
    Row,column: 3,4
    
    4 0   7 6 8
    0 5     5 5
    2 1       6
    4 1 3 7