python - 检查矩阵中其他值的对角线值的问题。(Python)
问题描述
所以我和我的朋友试图在 python 中重新创建 Conway 的生活游戏,但在尝试检查单元矩阵中对角相邻的值时遇到了问题。我们的代码查找与所讨论的值对角线的值,但由于某种原因,它似乎无法找到它们。例如,具有 3 个相邻单元(2 个相邻单元和 1 个对角线)的单元将作为 2 个相邻单元返回。为了调试,我们让它列出了所有活细胞的coridinates及其邻居计数。这是我们的代码:
initial_frame = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
next_frame = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
row = []
neighborcount = 0
next_frame = initial_frame
while True:
for e in range(1, 10):
for a in range(1, 10):
row.append(initial_frame[e][a])
print(row)
row = []
print("\n\n\n\n")
input()
for i in range(1, 10):
for o in range(1, 10):
neighborcount = 0
#Down 1
if initial_frame[(o + 1)][i] == 1:
neighborcount += 1
#Up 1
if initial_frame[(o - 1)][i] == 1:
neighborcount += 1
#Right 1
if initial_frame[o][(i + 1)] == 1:
neighborcount += 1
#Left 1
if initial_frame[o][(i - 1)] == 1:
neighborcount += 1
#Down 1, Right 1
if initial_frame[(o + 1)][(i + 1)] == 1:
neighborcount += 1
#Down 1, Left 1
if initial_frame[(o + 1)][(i - 1)] == 1:
neighborcount += 1
#Up 1, Left 1
if initial_frame[(o - 1)][(i - 1)] == 1:
neighborcount += 1
#Up 1, Right 1
if initial_frame[(o - 1)][(i + 1)] == 1:
neighborcount += 1
#If dead cell has exactly 3 neighbors, set it to be born
if initial_frame[o][i] == 0 and neighborcount == 3:
next_frame[o][i] = 1
#If living cell:
if initial_frame[o][i] == 1:
#does not have either 2 or 3 neighbors, set it to die
if neighborcount != 2 and neighborcount != 3:
next_frame[o][i] = 0
print(str(o) + ", " + str(i) + ": " + str(neighborcount))
#reset neighbors
neighborcount = 0
#Project set values onto real board
initial_frame = next_frame
解决方案
问题是,在这一行
next_frame = initial_frame
Python 实际上并不复制整个数组。next_frame
刚开始引用initial_frame
正在引用的任何内容,因此next_frame is initial_frame
将返回 true。
这可以通过在生成计算结束时交换数组来解决。像这样:
@@ -30,8 +30,6 @@ row = []
neighborcount = 0
-next_frame = initial_frame
-
while True:
for e in range(1, 10):
@@ -120,6 +118,8 @@ while True:
#If dead cell has exactly 3 neighbors, set it to be born
+ next_frame[o][i] = initial_frame[o][i]
+
if initial_frame[o][i] == 0 and neighborcount == 3:
next_frame[o][i] = 1
@@ -143,5 +143,6 @@ while True:
neighborcount = 0
#Project set values onto real board
-
+ garbage_arr = initial_frame
initial_frame = next_frame
+ next_frame = garbage_arr
结果代码:
initial_frame = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
next_frame = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
row = []
neighborcount = 0
while True:
for e in range(1, 10):
for a in range(1, 10):
row.append(initial_frame[e][a])
print(row)
row = []
print("\n\n\n\n")
input()
for i in range(1, 10):
for o in range(1, 10):
neighborcount = 0
#Down 1
if initial_frame[(o + 1)][i] == 1:
neighborcount += 1
#Up 1
if initial_frame[(o - 1)][i] == 1:
neighborcount += 1
#Right 1
if initial_frame[o][(i + 1)] == 1:
neighborcount += 1
#Left 1
if initial_frame[o][(i - 1)] == 1:
neighborcount += 1
#Down 1, Right 1
if initial_frame[(o + 1)][(i + 1)] == 1:
neighborcount += 1
#Down 1, Left 1
if initial_frame[(o + 1)][(i - 1)] == 1:
neighborcount += 1
#Up 1, Left 1
if initial_frame[(o - 1)][(i - 1)] == 1:
neighborcount += 1
#Up 1, Right 1
if initial_frame[(o - 1)][(i + 1)] == 1:
neighborcount += 1
#If dead cell has exactly 3 neighbors, set it to be born
next_frame[o][i] = initial_frame[o][i]
if initial_frame[o][i] == 0 and neighborcount == 3:
next_frame[o][i] = 1
#If living cell:
if initial_frame[o][i] == 1:
#does not have either 2 or 3 neighbors, set it to die
if neighborcount != 2 and neighborcount != 3:
next_frame[o][i] = 0
print(str(o) + ", " + str(i) + ": " + str(neighborcount))
#reset neighbors
neighborcount = 0
#Project set values onto real board
garbage_arr = initial_frame
initial_frame = next_frame
next_frame = garbage_arr
推荐阅读
- python - opencv2 - python - Inpaint 不使用 Jpg
- node.js - webdriverio 5.16 browser.element 不是函数
- c++ - 如何在使用替代解析器将输入分解为一系列不同部分时保持空格字符?
- html - 如何在 R html_nodes 中传递带有 ID 或类名的 CSS 选择器?
- c# - 抛出异常:Emgu.CV.dll 中的“System.TypeInitializationException”
- r - 创建 2 个垂直堆叠的侧边栏面板,一个带有标签,一个没有
- ios - Xcode 下载的设备应用程序容器提供了空的 Core Data .sqlite
- javascript - React hook,发生 Invalid hook call 错误
- protractor - 如何在量角器测试脚本中使用 waitForAngular()?
- php - 使用数据库中的坐标在地图上添加标记,使用 JSON 检索数据