【发布时间】:2016-03-22 16:38:22
【问题描述】:
我正在尝试制作列表的浅表副本,此列表存储在对象的属性中。即使我的浅拷贝在一个不起作用的函数中,我尝试了
copy.copy
temp = list(actuallist)
temp = actuallist[:]
这是我当前代码的相关部分
这是对象
class Game:
tiles = []
daleks = []
rawtile = ""
height = 20
width = 20
tilesize = 32
gamemap = "default"
status = 0
doctor = ""
screen = ""
clock = pygame.time.Clock()
def __init__(self,height,width,tilesize,gamemap):
self.height = height
self.width = width
self.tilesize = 32
self.gamemap = gamemap
self.status = 0
size = (tilesize*width, tilesize*height)
self.screen = pygame.display.set_mode(size)
pygame.display.set_caption("Doctor Who vs Daleks")
def teleportDoctorIn(self,classname):
self.doctor = classname
def releaseDalek(self,x):
self.daleks.append(x)
def resetDaleks(self):
daleks = []
这是我创建一个浅表并对其进行更改的部分
def updateMap(x,y):
temp = game.tiles[:]
"""SET DOCTOR COORDS"""
temp[game.doctor.xpos][game.doctor.ypos] = "X"
game.doctor.move(x,y)
temp[game.doctor.xpos][game.doctor.ypos] = "D"
"""LETS MOVE DALEKS"""
原来我需要复制.deepcopy() 列表。
【问题讨论】:
-
请详细说明它不起作用。它怎么不工作?你收到错误了吗?意想不到的结果?什么都没有?
-
请从您的代码中删除所有不需要展示问题性质的内容。题目很简单,题怎么这么长?请阅读如何创建minimal example。
-
temp[game.doctor.xpos][game.doctor.ypos] = "X"- 浅拷贝为您提供了一个新列表,但它并没有复制成员本身。temp[game.doctor.xpos]的对象与game.tiles[game.doctor.xpos]的对象相同。我不知道你的意图是什么,但也许copy.deepcopy是你想要的。 -
tdelaney,感谢您的回答。我现在明白我哪里做错了。