班级中的Python True副本
由于某种原因,我有一个列表,尽管明确地是深层副本,但仍在不断修改。它似乎在通过循环时没有被修饰,但是一旦存在,它突然被修改了吗?我一定缺少与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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,本地变量
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, notself.availible_moves
. You are saving a deepcopy ofself.availible_moves
and it is being stored in thelist_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 resetself.availible_moves
to be equal tolist_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 isself.availible_moves
. Notice the extra letter i in the first expression. This definitely is one of your problems.