python count变量在调用主函数时未在我的主函数中更新

时间:2018-08-09 07:17:24

标签: python python-3.x variables

我想做的是计算矩阵中与1相邻的1的数量,然后当我对某个[x] [y]进行计数时,我对相邻的数量进行计数并将其保存在一个名为matrix_qualities的相同矩阵

现在我面临的问题是,当if循环将邻居识别为1时,它应该计算邻居的数量,然后将其放入矩阵中,但是当我回叫它时,该计数不会在主目录中得到更新比较下一个邻居的功能

def compare(x, y, count, Matrix, rows, columns):
    while x >= 0 and y >= 0 and x <= (int(rows) - 1) and y <= (int(columns) - 1):
        if Matrix[x][y] == 1:
            count += 1
            break
        break
    return count


def main():
    String = input("Enter the example")
    _list = String.split(" ")

    print(_list)

    rows = _list[0]
    columns = _list[1]

    Matrix = [[0 for x in range(int(columns))] for y in range(int(rows))]

    z = 2

    for x in range(0, int(rows)):
        for y in range(0, int(columns)):
            Matrix[x][y] = int(_list[z])
            z += 1

    Matrix_qualityies = [[0 for x in range(int(columns))] for y in range(int(rows))]

    for x in range(0, int(rows)):
        for y in range(0, int(columns)):
            count = 0
            if Matrix[x][y] == 0:
                Matrix_qualityies[x][y] = 0
            if Matrix[x][y] == 1:
                if x >= 1:
                    compare(x - 1, y, count, Matrix, rows, columns)
                if x >= 1 and y >= 1:
                    compare(x - 1, y - 1, count, Matrix, rows, columns)
                if x >= 1 and y <= (int(columns) - 1):
                    compare(x - 1, y + 1, count, Matrix, rows, columns)
                if x <= (int(rows) - 1):
                    compare(x + 1, y, count, Matrix, rows, columns)
                if x <= (int(rows) - 1) and y >= 1:
                    compare(x + 1, y - 1, count, Matrix, rows, columns)
                if x <= (int(rows) - 1) and y <= (int(columns) - 1):
                    compare(x + 1, y + 1, count, Matrix, rows, columns)
                if y >= 1:
                    compare(x, y - 1, count, Matrix, rows, columns)
                if y <= (int(columns) - 1):
                    compare(x, y + 1, count, Matrix, rows, columns)
            Matrix_qualityies[x][y] = count

    print(Matrix)
    print(Matrix_qualityies)


main()

输入为2 9 1 0 1 1 0 1 1 1 1 0 0 0 1 0 1 0 0 1

1 个答案:

答案 0 :(得分:0)

您可以通过将其提取到辅助函数中来简化邻居的比较和累积:

也许是这样?

import random

NEIGHBOR_OFFSETS = [(1, 1), (1, -1), (1, 0), (-1, 0), (-1, 1), (-1, -1), (0, 1), (0, -1)]


def get_moore_neighbors_sum(array_of_arrays, row, col):
    sum_neighbors = 0
    for neighbor in NEIGHBOR_OFFSETS:
        dr, dc = neighbor
        try:
            if row + dr >= 0 and col + dc >= 0:
                sum_neighbors += array_of_arrays[row+dr][col+dc]
        except IndexError:
            continue
    return sum_neighbors


rows, cols = 5, 4
mat = [[random.choice([0, 1]) for c in range(cols)] for r in range(rows)]
mat_qualities = [[get_moore_neighbors_sum(mat, r, c) for c, _ in enumerate(row)] for r, row in enumerate(mat)] 

for row in mat:
    print(row)

print()

for row in mat_qualities:
    print(row)

输出示例:

[1, 1, 1, 0]
[1, 1, 0, 1]
[1, 1, 0, 0]
[1, 1, 1, 1]
[1, 0, 1, 1]

[3, 4, 3, 2]
[5, 6, 5, 1]
[5, 6, 6, 3]
[4, 6, 5, 3]
[2, 5, 4, 3]