在用python编写游戏时,我的部分代码出现了问题。在一些调试过程中,我遇到了一些非常奇怪的事情,我无法解释。我所显示的第一个代码将某个值打印到数组‘多边形’。第二个代码相同,但在最后,我将第一个代码打印的值赋给数组‘多边形’。显然,我希望程序的其余部分(我没有显示)会以同样的方式处理第一或第二段代码,因为它们最终会给出“多边形”相同的值。然而,第二个代码使程序的其余部分正常工作,但第一个代码不能工作。
为了我的生命,我不知道发生了什么事。我不提供项目的其余部分,因为我认为这将是多余的,因为在第一和第二种情况是相同的。
有人知道是什么导致了这一切吗?谢谢你的帮助。
第一个代码:
if 1:
dimensions = 3
polygon = [[[100,100],[100,-100]],[[100,-100],[-100,-100]],[[-100,-100],[-100,100]],[[-100,100],[100,100]]]
for limeJuice in range(dimensions-2):
p = copy.deepcopy(polygon)
for j in range(len(p)):
p[j][0].append(100)
p[j][1].append(100)
bob = copy.deepcopy(polygon)
for j in range(len(bob)):
bob[j][0].append(-100)
bob[j][1].append(-100)
q = []
for j in range(len(p)):
q.append([p[j][0],bob[j][0]])
polygon = []
for pearJuice in p: polygon.append(pearJuice)
for pearJuice in bob: polygon.append(pearJuice)
for pearJuice in q: polygon.append(pearJuice)
print(polygon)
#rest of the programme
#--------------------------------------------------------------------#
#second code:
if 1:
dimensions = 3
polygon = [[[100,100],[100,-100]],[[100,-100],[-100,-100]],[[-100,-100],[-100,100]],[[-100,100],[100,100]]]
for limeJuice in range(dimensions-2):
p = copy.deepcopy(polygon)
for j in range(len(p)):
p[j][0].append(100)
p[j][1].append(100)
bob = copy.deepcopy(polygon)
for j in range(len(bob)):
bob[j][0].append(-100)
bob[j][1].append(-100)
q = []
for j in range(len(p)):
q.append([p[j][0],bob[j][0]])
polygon = []
for pearJuice in p: polygon.append(pearJuice)
for pearJuice in bob: polygon.append(pearJuice)
for pearJuice in q: polygon.append(pearJuice)
polygon = [[[100, 100, 100], [100, -100, 100]], [[100, -100, 100], [-100, -100, 100]], [[-100, -100, 100], [-100, 100, 100]], [[-100, 100, 100], [100, 100, 100]], [[100, 100, -100], [100, -100, -100]], [[100, -100, -100], [-100, -100, -100]], [[-100, -100, -100], [-100, 100, -100]], [[-100, 100, -100], [100, 100, -100]], [[100, 100, 100], [100, 100, -100]], [[100, -100, 100], [100, -100, -100]], [[-100, -100, 100], [-100, -100, -100]], [[-100, 100, 100], [-100, 100, -100]]]
#rest of the programme发布于 2019-08-22 06:46:13
TLDR:第一个变体在数据之间共享引用,第二个变体没有。
list文本或等式都不表示元素与其他数据结构的共享。两个具有相同文本或相等的对象仍然允许它们具有不同的共享数据。当您修改对象时,这会产生不同的效果:
>>> a = [[1, 2, 3], [4, 5, 6]]
>>> # copy by sharing elements
>>> b = [a[0], a[1]]
>>> # copy by literal value
>>> c = eval(repr(a))
>>> a == b == c
True
>>> a[1].append(7)
>>> a == b # b shares data with a
True
>>> a == c # c shares no data
False在您的示例中,第一个polygon与其他列表共享其数据。从文本创建的第二个polygon只包含新对象:
# share objects with polygon
for pearJuice in p: polygon.append(pearJuice)
for pearJuice in bob: polygon.append(pearJuice)
for pearJuice in q: polygon.append(pearJuice)
# create new objects for polygon
polygon = [[[100, 100, 100], [100, -100, 100]], [[100, -100, 100], [-100, -100, 100]], [[-100, -100, 100], [-100, 100, 100]], [[-100, 100, 100], [100, 100, 100]], [[100, 100, -100], [100, -100, -100]], [[100, -100, -100], [-100, -100, -100]], [[-100, -100, -100], [-100, 100, -100]], [[-100, 100, -100], [100, 100, -100]], [[100, 100, 100], [100, 100, -100]], [[100, -100, 100], [100, -100, -100]], [[-100, -100, 100], [-100, -100, -100]], [[-100, 100, 100], [-100, 100, -100]]]如果您需要在程序中分别修改q、bob、p和polygon,请记住对它们进行copy.deepcopy。
https://stackoverflow.com/questions/57598028
复制相似问题