Python的不断更新阵列 [英] Python continuously updating array

查看:124
本文介绍了Python的不断更新阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python解决数独的写了一个简单的回溯算法,一会儿有困难,因为它似乎更新不应该被更新的阵列,从而导致整个程序备份。

从本质上讲,作为code的一部分,我存储在初始值的数组,然后取得数组的副本,这样,当我回溯和分配新值,我没有改变任何数字的在原始数独给予。但是,在这个过程中我莫名其妙地更新我最初的数字数组。我终于想通了,它正在由线而引起格=初始,它应该有我的工作网格(网格)设置为初始值(初始),但只叫在程序的开始。当我把该线路输出和手动分配电网同样的事情作为初始(通过复制/粘贴),该程序正常工作。

我包括我下面的全code,有人知道为什么该行可能已经得到再次呼吁?我不明白。

 初始= [0,0,0,0,7,0,1,4,0​​]
         [2,0,0,0,1,5,3,0,6]
         [0,6,1,0,3,0,0,0,8]
         [0,0,0,7,0,0,0,5,3]
         [0,0,9,0,0,0,8,0,0]
         [0,7,0,0,0,1,0,0,0]
         [1,0,0,0,4,0,0,9,0]
         [9,0,6,1,0,0,0,0,2]
         [0,2,3,0,9,0,0,0,0]#系统下面一行曾经是格=初始格= [[0,0,0,0,7,0,1,4,0​​],
         [2,0,0,0,1,5,3,0,6]
         [0,6,1,0,3,0,0,0,8]
         [0,0,0,7,0,0,0,5,3]
         [0,0,9,0,0,0,8,0,0]
         [0,7,0,0,0,1,0,0,0]
         [1,0,0,0,4,0,0,9,0]
         [9,0,6,1,0,0,0,0,2]
         [0,2,3,0,9,0,0,0,0]
高清printBoard(网格):
    因为我在范围(0,8):
        打印(电网[I])高清checkValidity(NUM,行,列):
    如果num网格[行]:
        返回False
    因为我在范围(0,8):
        如果电网[I] [COL] == NUM​​:
            返回False
    cageRow =行// 3
    cageCol =山坳// 3
    因为我在范围(0,3):
        如果num网格[3 * cageRow + I] [3 * cageCol:3 * cageCol + 3]:
            返回False
    返回True高清nextCell(行,列,原路返回):
    如果原路返回== 0:
        如果COL == 8:
            行+ = 1
            山坳= 0
        其他:
            山坳+ = 1
    其他:
        如果COL == 0:
            按行= 1
            山坳= 8
        其他:
            协作= 1
    如果行℃的:
        打印(错误:退步太过分了。)
    回报(行,列)高清findNewNumber(行,列,NUM,原路返回):
    在范围I(NUM + 1,10):
        如果checkValidity(I,行,列):
            返回(I,0)
    返回(0,1)
行= 0
山坳= 0
原路返回= 0
打印(解决......)而排< 9:
    如果电网[行] [COL] ==初始[行] [COL]和初始[行] [山口] = 0!
        [行,列] = nextCell(行,列,原路返回)
    其他:
        NUM =网格[行] [COL]
        格[行] [COL] = 99
        [NUM,原路返回] = findNewNumber(行,列,NUM,原路返回)
        格[行] [山口] = NUM
        [行,列] = nextCell(行,列,原路返回)打印(解决了!)
printBoard(初始)
打印()
printBoard(网格)


解决方案

 格=初始
ID(初始)== ID(网格)
>>>真正

这不会设置电网相同的值初始。它使电网引用同一个对象初始。人们通常做的是使浅拷贝,这样

 格=初始[:]
ID(初始)== ID(网格)
>>>假

这不会工作你的情况虽然。这将创建一个新的名单外,将举行同样的内部列表。

  ID(初始[0])== ID(网格[0])
>>>真正

您需要的是 deepcopy的来获取内部列表的拷贝。

 进口副本格= copy.deepcopy(初始)

然后一切都被复制

  ID(初始)== ID(网格)
>>>假
ID(初始[0])== ID(网格[0])
>>>假

I wrote a simple backtracking algorithm in python to solve sudoku's, and for a while had difficulty because it seemed to update an array that should not have been updated, which caused the entire program to backup.

Essentially, as part of the code, I stored an array of the initial values and then made a copy of the array so that when I was backtracking and assigning new values, I didn't change any of the numbers that were given in the original sudoku. However, in the process I somehow updated my array of initial numbers. I eventually figured out it was being caused by the line grid=initial, which should have set my working grid (grid) to the initial values (initial) but was only called at the start of the program. When I took that line out and manually assigned grid to the same thing as initial (via copy/paste), the program worked fine.

I've included my full code below, anybody know why that line may have been getting called again? I can't figure it out.

initial=[[0,0,0,0,7,0,1,4,0],
         [2,0,0,0,1,5,3,0,6],
         [0,6,1,0,3,0,0,0,8],
         [0,0,0,7,0,0,0,5,3],
         [0,0,9,0,0,0,8,0,0],
         [0,7,0,0,0,1,0,0,0],
         [1,0,0,0,4,0,0,9,0],
         [9,0,6,1,0,0,0,0,2],
         [0,2,3,0,9,0,0,0,0]]

#The following line used to be grid=initial

grid=[[0,0,0,0,7,0,1,4,0],
         [2,0,0,0,1,5,3,0,6],
         [0,6,1,0,3,0,0,0,8],
         [0,0,0,7,0,0,0,5,3],
         [0,0,9,0,0,0,8,0,0],
         [0,7,0,0,0,1,0,0,0],
         [1,0,0,0,4,0,0,9,0],
         [9,0,6,1,0,0,0,0,2],
         [0,2,3,0,9,0,0,0,0]]


def printBoard(grid):
    for i in range(0,8):
        print(grid[i])

def checkValidity(num,row,col):
    if num in grid[row]:
        return False


    for i in range(0,8):
        if grid[i][col]==num:
            return False


    cageRow=row//3
    cageCol=col//3
    for i in range(0,3):
        if num in grid[3*cageRow+i][3*cageCol:3*cageCol+3]:
            return False


    return True

def nextCell(row,col,backtrack):
    if backtrack==0:
        if col==8:
            row+=1
            col=0
        else:
            col+=1
    else:
        if col==0:
            row-=1
            col=8
        else:
            col-=1
    if row<0:
        print("Error: Backtracked too far.")
    return (row,col)

def findNewNumber(row,col,num,backtrack):
    for i in range(num+1,10):
        if checkValidity(i,row,col):
            return (i,0)
    return (0,1)


row=0
col=0
backtrack=0
print("Solving...")

while row<9:
    if grid[row][col]==initial[row][col] and initial[row][col]!=0:
        [row,col]=nextCell(row,col,backtrack)
    else:
        num=grid[row][col]
        grid[row][col]=99
        [num,backtrack]=findNewNumber(row,col,num,backtrack)
        grid[row][col]=num
        [row,col]=nextCell(row,col,backtrack)

print("Solved!")
printBoard(initial)
print("")
printBoard(grid)

解决方案

grid = initial
id(initial) == id(grid)
>>> True

This doesn't set grid to the same values as initial. It makes grid refer to the same object as initial. What people commonly do is make a shallow copy, like this

grid = initial[:]
id(initial) == id(grid)
>>> False

This will not work in your case though. It would create a new outer list that will hold the same interior lists.

id(initial[0]) == id(grid[0])
>>> True

What you need is a deepcopy to get copies of the interior lists as well.

import copy

grid = copy.deepcopy(initial)

then everything is copied

id(initial) == id(grid)
>>> False
id(initial[0]) == id(grid[0])
>>> False

这篇关于Python的不断更新阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆