Python,制作列表的浅拷贝并不';不能使用对象属性吗?

Python,制作列表的浅拷贝并不';不能使用对象属性吗?,python,list,object,Python,List,Object,我试图创建列表的浅层副本,该列表存储在对象的属性中。尽管我的浅拷贝在一个函数中,但它不起作用,我还是尝试了 copy.copy temp = list(actuallist) temp = actuallist[:] 下面是我当前代码的相关部分 这就是目标 class Game: tiles = [] daleks = [] rawtile = "" height = 20 width = 20 tilesize = 32 gam

我试图创建列表的浅层副本,该列表存储在对象的属性中。尽管我的浅拷贝在一个函数中,但它不起作用,我还是尝试了

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()列表。

你的三种技术只是简单地复制列表。因此,即使列表本身是unque(您可以在一个列表上添加和删除元素而不影响另一个),但包含的对象是相同的
temp[game.doctor.xpos][game.doctor.ypos]=“X”
更改两个列表仍保留的包含对象

例如,让我们把一些
dict
s放在一个列表中,看看会发生什么

>>> import copy
>>> d1={1:'one', 2:'two'}
>>> d2={3:'three', 4:'four'}
# create a list then copy it
>>> l1=[d1, d2]
>>> l2=l1[:]

# l1 and l2 are different, but their members are the same
>>> id(l1) == id(l2)
False
>>> id(l1[0]) == id(l2[0])
True

# and if you change the objects in l2, they change l1
>>> l1[0]
{1: 'one', 2: 'two'}
>>> l2[0][5]='five'
>>> l1[0]
{1: 'one', 2: 'two', 5: 'five'}

# but a deep copy copies the contained objects also so no problem
>>> l3=copy.deepcopy(l1)

# the contained objects are not the same
>>> id(l1[0]) == id(l3[0])
False

# and changing one does not affect the other
>>> l1[0]
{1: 'one', 2: 'two', 5: 'five'}
>>> l3[0]
{1: 'one', 2: 'two', 5: 'five'}
>>> l3[0][6] = 'six'
>>> 
>>> l1[0]
{1: 'one', 2: 'two', 5: 'five'}
>>> l3[0]
{1: 'one', 2: 'two', 5: 'five', 6: 'six'}
>>> 

请详细说明它不起作用。它怎么不起作用?你有错误吗?意外的结果?什么都没有?请从代码中删除显示问题性质不需要的所有内容。题目很简单,为什么问题这么长?请阅读如何创建一个。
temp[game.doctor.xpos][game.doctor.ypos]=“X”
-一个浅显副本为您提供了一个新列表,但它没有复制成员本身。
temp[game.doctor.xpos]
上的对象与
game.tiles[game.doctor.xpos]
上的对象相同。我不知道你的意图是什么,但也许
copy.deepcopy
是你想要的。t谢谢你的回答。我现在明白我哪里做错了。复制20x20平铺列表会对我的代码产生多大的影响?我正在做一个简单的游戏,charachter一次移动一步,对于每一步,我都需要做这个深度复制。你可以使用
timeit
模块来运行实验,但我认为这对于20x20阵列来说只是一个小效果,我不会担心。特别是如果每个步骤都是由用户启动的,那么按下[code>enter键所需的时间比较长。在我的代码中,每个移动都是用箭头键完成的,因此当用户单击KeyUp时,角色的位置向上移动1个方块,然后地图中的“敌人”向主角色移动1个方块,然后,当所有的人工智能和计算完成后,我复制回平铺以显示在屏幕上,我使用pygame模块来可视化平铺等。我对编程相当陌生,这是我在python中的第一堂课,但我想接受挑战。