在将python count变量调用回主函数时,它没有在我的主函数中更新
我想做的是计算矩阵中与1相邻的1的数量,然后当我计算某个[x][y]时,我计算相邻的数量,并将它们保存在一个名为matrix_的相同矩阵中 现在我面临的问题是,当if循环将邻居识别为1时,它应该计算邻居的数量,然后将其放入矩阵中,但当我在函数中调用它以比较下一个邻居时,计数在主循环中没有得到更新在将python count变量调用回主函数时,它没有在我的主函数中更新,python,python-3.x,variables,Python,Python 3.x,Variables,我想做的是计算矩阵中与1相邻的1的数量,然后当我计算某个[x][y]时,我计算相邻的数量,并将它们保存在一个名为matrix_的相同矩阵中 现在我面临的问题是,当if循环将邻居识别为1时,它应该计算邻居的数量,然后将其放入矩阵中,但当我在函数中调用它以比较下一个邻居时,计数在主循环中没有得到更新 def compare(x, y, count, Matrix, rows, columns): while x >= 0 and y >= 0 and x <= (int(r
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()
def比较(x、y、计数、矩阵、行、列):
当x>=0和y>=0和x=1和y>=1时:
比较(x-1,y-1,计数,矩阵,行,列)
如果x>=1且y,则可以通过将其提取到辅助函数来简化邻居的比较和累积:
也许是这样的
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)
输出示例:
修复缩进。编译时没有问题,我会在本页上尝试修复它。我修复了缩进。您不需要对compare
函数的返回值执行任何操作。非常感谢您提醒我,我得到了答案。我实际上仍处于python的学习阶段,因此我不知道如何使用random
或try
和除了
之外,但是你的代码比我的代码效率高很多,因为我可以看到我仍在试图理解try和exception块感谢你的回答random是为了生成一些虚拟数据用于测试目的;try/except是python的惯用用法,通常认为请求原谅比请求允许更容易。卢夫:好的,深呼吸,让我们重新开始:(1)启动一个新的口译员,(2)复制/粘贴我发布的内容,(3)运行它,看看它是否有效。一旦完成,您就可以开始修改部件,并使其适合您的代码,您将到达那里。好的,很酷,做得好当这样的事情发生时,退一步想办法解决通常是有用的。。。使用spyder时,您需要小心,解释器会使以前运行的数据保持活动状态;有时,在修改代码之前,最好重新启动解释器以清除先前分配的数据。
[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]