Python全局变量本身会改变。。。。为什么?

2024-09-26 18:01:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python3.4.2制作了这个python程序。 一开始我定义了一个变量'best', 以后可以在函数中更改它(我没有忘记“全局最佳”)。 每次更改都会打印(“new best”,best) 在程序的最后,它再次打印出最佳值,但它与上次不同,它已经改变。。。这怎么可能???在

大多数代码对于这一点并不重要(我想),但是与best有关的行都用#########################

from time import clock


size = [int(i) for i in input().split()]
step = [int(i) for i in input().split()]
clock()

best = [[0 for i in range(size[1])] for i in range(size[0])]    ##########
best[0][0] = 1                  ##########
lenbest=1                       ##########

if step[0] == step[1]:
    xstep = step[0]
    moves = [[xstep,xstep],[-xstep,xstep],
             [xstep,-xstep],[-xstep,-xstep]]
    try:
        best[xstep][xstep]      ##########
        posToGo = [moves[0]]
    except: None

else:
    moves = [[step[0],step[1]],[step[1],step[0]],
             [-step[0],step[1]],[-step[1],step[0]],
             [step[0],-step[1]],[step[1],-step[0]],
             [-step[0],-step[1]],[-step[1],-step[0]]]
    try:
        best[step[0]][step[1]]  ##########
        posToGo = [moves[0]]
    except: None
    try:
        best[step[1]][step[0]]  ##########
        posToGo.append(moves[1])
    except: None

def attempt(board,posToGo,step):

    currentMoves = []
    for pos in posToGo:
        currentMoves.append([pos])
        for move in moves:
            try:
                x = pos[0]+move[0]
                y = pos[1]+move[1]
                if x>-1 and y>-1 and not board[x][y]:
                    currentMoves[-1].append([x,y])
            except:None
    currentMoves.sort(key=len)
    for move in currentMoves:
        nboard = board
        nboard[move[0][0]][move[0][1]] = step
        attempt(nboard,move[1:],step+1) ########## Note that i'm calling a function within itself
    if step>lenbest:                ##########
        print("new best with pathlength",step-1,"after",clock(),"secconds")
        global lenbest            ##########
        lenbest = step              ##########
        global best               ##########
        best = board                ##########
        print("in the def",best)    ########## This one differs from...


attempt(best,posToGo,2)             ##########
print("at the end",best)            ########## ... this one. ??!!

输入(例如):

^{2}$

我得到这个输出:

new best with pathlength 64 after 0.0017115690152521709 secconds
in the def [[1, 16, 63, 34, 3, 18, 21, 36], [64, 33, 2, 17, 52, 35, 4, 19], [15, 62, 49, 58, 45, 20, 37, 22], [32, 59, 44, 53, 48, 51, 42, 5], [61, 14, 57, 50, 43, 46, 23, 38], [28, 31, 60, 47, 54, 41, 6, 9], [13, 56, 29, 26, 11, 8, 39, 24], [30, 27, 12, 55, 40, 25, 10, 7]]
at the end [[1, 4, 3, 4, 3, 4, 5, 20], [4, 5, 2, 3, 4, 5, 4, 5], [3, 2, 13, 4, 3, 4, 7, 6], [14, 3, 4, 3, 10, 5, 4, 5], [3, 4, 3, 4, 5, 4, 5, 6], [4, 13, 4, 7, 4, 11, 6, 7], [13, 10, 13, 8, 7, 8, 7, 8], [14, 13, 8, 9, 10, 7, 8, 7]]

所以它是不同的,虽然它只改变了一次(数一数说“new best…”的行):


Tags: inboardnonenewformovestepbest
1条回答
网友
1楼 · 发布于 2024-09-26 18:01:10

当一个变量在一个函数中被定义时(就像你在函数attemptbest = board中所做的那样),这个变量会自动成为一个局部变量。这意味着在您的示例中,全局变量的值并没有改变,尽管在函数看来是这样的。在

若要能够写入全局变量,请添加以下语句

global best

在函数开始时。在

还要注意,通过将board分配给best,现在仍然只有一个数组被两个变量引用。使用模块copy中的函数deepcopy

^{pr2}$

你可以写:

best = deepcopy(board)

现在,输出为:

('new best with pathlength', 64, 'after', 0.040691, 'secconds')
('in the def', [[1, 16, 63, 34, 3, 18, 21, 36], [64, 33, 2, 17, 52, 35, 4, 19], [15, 62, 49, 58, 45, 20, 37, 22], [32, 59, 44, 53, 48, 51, 42, 5], [61, 14, 57, 50, 43, 46, 23, 38], [28, 31, 60, 47, 54, 41, 6, 9], [13, 56, 29, 26, 11, 8, 39, 24], [30, 27, 12, 55, 40, 25, 10, 7]])
('at the end', [[1, 16, 63, 34, 3, 18, 21, 36], [64, 33, 2, 17, 52, 35, 4, 19], [15, 62, 49, 58, 45, 20, 37, 22], [32, 59, 44, 53, 48, 51, 42, 5], [61, 14, 57, 50, 43, 46, 23, 38], [28, 31, 60, 47, 54, 41, 6, 9], [13, 56, 29, 26, 11, 8, 39, 24], [30, 27, 12, 55, 40, 25, 10, 7]])

也就是说,存储在best中的板副本没有被更改。在

相关问题 更多 >

    热门问题