Python 计算八邻域之和

#Python计算矩阵8邻域之和
Neighbors = [(1, 1), (1, -1), (1, 0), (-1, 0), (-1, 1), (-1, -1), (0, 1), (0, -1)]

def get_neighbors_sum(array_of_arrays, row, col):
    sum_neighbors = 0
    for neighbor in Neighbors:
        dr, dc = neighbor
        try:
            sum_neighbors += array_of_arrays[row+dr][col+dc]
        except IndexError:
            pass
    return sum_neighbors

array_of_arrays = #"Matrix"
rows = len(array_of_arrays)
cols = len(array_of_arrays[0])

for row in range(rows):
    for col in range(cols):
        sum_neighbors = get_neighbors_sum(array_of_arrays, row, c

 

你可能感兴趣的:(python)