gpt4 book ai didi

Python 深度列表比较

转载 作者:太空宇宙 更新时间:2023-11-04 03:58:53 25 4
gpt4 key购买 nike

我目前正在用 python 编写一个数独解决程序,只是为了好玩。这是我目前拥有的:

#!/usr/bin/env python
"""Reads in a file formatted with nine lines each of which has nine characters
corresponding to a sudoku puzzle. A blank is indicated by the value '0'
Eventually should output a solution to the input puzzle"""

import sys

class cell:
value = 0
"""Value of 0 means it is undetermined"""

def __init__(self, number):
self.value = number
self.possible = [2, 2, 2, 2, 2, 2, 2, 2, 2]
"""Possibility a given value can be the number. 0 is impossible, 1 is definite, 2 is maybe"""



def selfCheck(self):
"""Checks if the cell has only one possible value, changes the value to that number"""
if self.value == 0:
if self.possible.count(2) == 1:
"""If there's only one possible, change the value to that number"""
i = 1
for item in self.possible:
if item == 2:
self.value = i
self.possible[i-1] = 1
i+=1

def checkSection(section):
"""For any solved cells in a section, marks other cells as not being that value"""
for cell in section:
if cell.value != 0:
for otherCell in section:
otherCell.possible[cell.value-1] = 0

def checkChunk(chunk):
"""Checks a chunk, the set of rows, columns, or squares, and marks any values that are impossible for cells based on that
chunk's information"""
for section in chunk:
checkSection(section)

def selfCheckAll(chunk):
for section in chunk:
for cell in section:
cell.selfCheck()

cellRows = [[],[],[],[],[],[],[],[],[]]
cellColumns = [[],[],[],[],[],[],[],[],[]]
cellSquares = [[],[],[],[],[],[],[],[],[]]

infile = open(sys.argv[1], 'r')
"""Reads the file specified on the command line"""

i = 0
for line in infile:
"""Reads in the values, saves them as cells in 2d arrays"""
line = line.rstrip('\n')
for char in line:
row = i/9
column = i%9
newcell = cell(int(char))
cellRows[row].append(newcell)
cellColumns[column].append(newcell)
row = (row/3)*3
column = column/3
square = row+column
cellSquares[square].append(newcell)
i+=1
i = 0
while i<50:
checkChunk(cellRows)
checkChunk(cellColumns)
checkChunk(cellSquares)
selfCheckAll(cellRows)

i+=1

displayRow = []
for row in cellRows:
for cell in row:
displayRow.append(str(cell.value))

i = 0
while i < 9:
output1 = ''.join(displayRow[9*i:9*i+3])
output2 = ''.join(displayRow[9*i+3:9*i+6])
output3 = ''.join(displayRow[9*i+6:9*i+9])
print output1 + ' ' + output2 + ' ' + output3
if i%3 == 2:
print
i+=1

我的问题是:

i = 0
while i<50:
checkChunk(cellRows)
checkChunk(cellColumns)
checkChunk(cellSquares)
selfCheckAll(cellRows)

i+=1

我想运行代码,直到它检测到与之前的迭代相比没有变化,而不是当前硬编码的 50 次。这可能是因为不再有合乎逻辑的下一步(需要开始暴力破解值),或者难题已完全解决。无论哪种方式,我都需要一个当前数据集的深拷贝(比如 cellRows)来比较实际副本在通过我的 checkChunk 函数时可能发生的变化。

在 Python 中有这样的东西吗? (如果有更好的方法来检查我是否完成,那也可以,尽管此时我更感兴趣的是我是否可以进行深入比较。)

编辑 - 我尝试使用 copy.deepcopy。虽然这创建了一个很好的深拷贝,但使用“==”检查两者之间的相等性总是返回 false。

最佳答案

可以通过比较str() 来进行非常粗略的比较。这当然不是最好的方法,但考虑到列表的复杂性,这可能没问题。

如果你想要更可靠的东西,你可以写一个递归函数来处理它。

关于Python 深度列表比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16799962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com