Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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
Function moore邻域代码,用于查找具有1s的相邻单元数_Function_Python 3.x_Neighbours - Fatal编程技术网

Function moore邻域代码,用于查找具有1s的相邻单元数

Function moore邻域代码,用于查找具有1s的相邻单元数,function,python-3.x,neighbours,Function,Python 3.x,Neighbours,伙计们,我有一个任务,我必须找到网格中一个单元格周围的1数。从示例图中,我应该得到3,因为单元格周围有3个1。所以我做了代码,我做对了,但是当我用一个函数做同样的代码时,它会给我错误,我需要你的帮助 def count_neighbours(((1, 0, 0, 1, 0),(0, 1, 0, 0, 0),(0, 0, 1, 0, 1),(1, 0, 0, 0, 0),(0, 0, 1, 0, 0),), 1, 2): grid=count_neighbours[0] row=count_nei

伙计们,我有一个任务,我必须找到网格中一个单元格周围的1数。从示例图中,我应该得到3,因为单元格周围有3个1。所以我做了代码,我做对了,但是当我用一个函数做同样的代码时,它会给我错误,我需要你的帮助

def count_neighbours(((1, 0, 0, 1, 0),(0, 1, 0, 0, 0),(0, 0, 1, 0, 1),(1, 0, 0, 0, 0),(0, 0, 1, 0, 0),), 1, 2):
grid=count_neighbours[0]
row=count_neighbours[1]
col=count_neighbours[2]
length=len(grid)
s=0
d=0
for i in range(row-1,row+2):
    for j in range(col-1,col+2):
        if i>=0 and j>=0:
            if i<=length-1 and j<=length-1:
                if grid[i][j]==1:
                    s+=1
if grid[row][col]==1:
    d+=1
total = s-d
return total
#error goes like this
def count_neighbours(((1, 0, 0, 1, 0),(0, 1, 0, 0, 0),(0, 0, 1, 0, 1),(1, 0, 0, 0, 0),(0, 0, 1, 0, 0),), 1, 2):
                     ^
SyntaxError: invalid syntax
def count_近邻((1,0,0,1,0),(0,1,0,0),(0,0,1,0,1),(1,0,0,0,0),(0,0,1,0,0),)1,2):
网格=计数\u邻居[0]
行=计数\u邻居[1]
col=计数\u邻居[2]
长度=长度(网格)
s=0
d=0
对于范围内的i(第1行,第+2行):
对于范围内的j(第1列,第2列):
如果i>=0和j>=0:

如果我请查看python中函数定义的基础知识。不应将输入值放入函数定义中

我觉得这就是你要找的

def count_neighbours(inputgrid):
    grid=inputgrid[0]
    row=inputgrid[1]
    col=inputgrid[2]
    length=len(grid)
    s=0
    d=0
    for i in range(row-1,row+2):
        for j in range(col-1,col+2):
            if i>=0 and j>=0:
                if i<=length-1 and j<=length-1:
                    if grid[i][j]==1:
                        s+=1
    if grid[row][col]==1:
        d+=1
    total = s-d
    print (total)
    return total


count_neighbours((((1, 0, 0, 1, 0),(0, 1, 0, 0, 0),(0, 0, 1, 0, 1),(1, 0, 0, 0, 0),(0, 0, 1, 0, 0),), 1, 2))
def count_邻居(inputgrid):
网格=输入网格[0]
行=输入网格[1]
col=输入网格[2]
长度=长度(网格)
s=0
d=0
对于范围内的i(第1行,第+2行):
对于范围内的j(第1列,第2列):
如果i>=0和j>=0:

如果我请查看python中函数定义的基础知识。不应将输入值放入函数定义中

我觉得这就是你要找的

def count_neighbours(inputgrid):
    grid=inputgrid[0]
    row=inputgrid[1]
    col=inputgrid[2]
    length=len(grid)
    s=0
    d=0
    for i in range(row-1,row+2):
        for j in range(col-1,col+2):
            if i>=0 and j>=0:
                if i<=length-1 and j<=length-1:
                    if grid[i][j]==1:
                        s+=1
    if grid[row][col]==1:
        d+=1
    total = s-d
    print (total)
    return total


count_neighbours((((1, 0, 0, 1, 0),(0, 1, 0, 0, 0),(0, 0, 1, 0, 1),(1, 0, 0, 0, 0),(0, 0, 1, 0, 0),), 1, 2))
def count_邻居(inputgrid):
网格=输入网格[0]
行=输入网格[1]
col=输入网格[2]
长度=长度(网格)
s=0
d=0
对于范围内的i(第1行,第+2行):
对于范围内的j(第1列,第2列):
如果i>=0和j>=0:
如果我