班级中的Python True副本

发布于 2025-02-01 22:05:09 字数 1268 浏览 2 评论 0原文

由于某种原因,我有一个列表,尽管明确地是深层副本,但仍在不断修改。它似乎在通过循环时没有被修饰,但是一旦存在,它突然被修改了吗?我一定缺少与Python的规则和逻辑有关的东西,但我无法为自己的一生弄清楚。

def all_possible_states(self):
    #creates many variations of a board from the available set of moves
    to_return = [] #list of possible board states to return
    #print(self.availible_moves)
    list_to_copy = copy.deepcopy(self.availible_moves)
    for item in self.availible_moves:
        print('loop')
        
        #append possible board state to list. set of availible moves for that board is one less. Done by removing item from that move list
        to_return.append(board(self.player, copy.deepcopy(self.board.copy()), list_to_copy, self.plays, self.score, copy.deepcopy(item)))
        
        #print(item)
        print( self.avalible_moves) #shows the total set of moves. This is unmodified whenever it prints
        print(list_to_copy)#deep copy of the original list. This is unmodified when it prints
        print(to_return[len(to_return) - 1].availible_moves) #List of moves left availible for the board, this slowly shrinks for some reason each loop
    
    
    print(self.availible_moves) #this is the original list, but it's not basically been cut all the way down for some reason
    return to_return

For some reason, I have a list that keeps getting modified despite being explicitly a deep copy. It appears to be unmodified as it goes through the loop, but it suddenly is modified once it exists? I must be missing something that pertains to Python's rules and logic, but I can't figure it out for the life of me.

def all_possible_states(self):
    #creates many variations of a board from the available set of moves
    to_return = [] #list of possible board states to return
    #print(self.availible_moves)
    list_to_copy = copy.deepcopy(self.availible_moves)
    for item in self.availible_moves:
        print('loop')
        
        #append possible board state to list. set of availible moves for that board is one less. Done by removing item from that move list
        to_return.append(board(self.player, copy.deepcopy(self.board.copy()), list_to_copy, self.plays, self.score, copy.deepcopy(item)))
        
        #print(item)
        print( self.avalible_moves) #shows the total set of moves. This is unmodified whenever it prints
        print(list_to_copy)#deep copy of the original list. This is unmodified when it prints
        print(to_return[len(to_return) - 1].availible_moves) #List of moves left availible for the board, this slowly shrinks for some reason each loop
    
    
    print(self.availible_moves) #this is the original list, but it's not basically been cut all the way down for some reason
    return to_return

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

圈圈圆圆圈圈 2025-02-08 22:05:09

请注意,本地变量list_to_copy是deepcopy,而不是self.availible_moves。您要保存self.availible_moves的deepcopy,并将其存储在您定义的list_to_copy 变量中。 list_to_copy永远不会像deepcopy所预期的那样更改。我不确定您要做什么,但是如果您愿意,最终您可以重置self.availible_moves等于list_to_copy ,然后它将好像从未改变。

编辑:

实际上,我认为您有一个拼写错误,请注意您print(self.avalible_moves),而您说的是不改变,而实际上在更改的是<代码> self.availible_moves 。请注意第一个表达中的额外字母i。这绝对是您的问题之一。

Notice, the local variable list_to_copy is the deepcopy, not self.availible_moves. You are saving a deepcopy of self.availible_moves and it is being stored in the list_to_copy variable you defined. list_to_copy never changes as expected from a deepcopy. I'm not exactly sure what you are trying to do, but if you want, at the end you can reset self.availible_moves to be equal to list_to_copy and then it will be as if it never changed.

EDIT:

Actually, I think you have a spelling mistake, notice that you print(self.avalible_moves) and you are saying its not changing, when really what is changing is self.availible_moves. Notice the extra letter i in the first expression. This definitely is one of your problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文