简体   繁体   English

python 错误列表参考

[英]python erroneous list reference

Hello community,你好社区,

        SourcePoints = self.SourcePoints
        TargetPoints = self.TargetPoints

        print("SourcePoints: ", self.SourcePoints)
        print("TargetPoints: ", self.TargetPoints)

        for point in SourcePoints:
            for i in range(len(point)):
                point[i] = Origin[i] + point[i] * Spacing[i]
        for point in TargetPoints:
            for i in range(len(point)):
                point[i] = Origin[i] + point[i] * Spacing[i]

        print("SourcePoints: ", self.SourcePoints)
        print("TargetPoints: ", self.TargetPoints)

What you probably need to know for context: - self.SourcePoints/self.TargetPoints is initialized once an instance of the class (that inherits the code) is created.您可能需要了解上下文: - self.SourcePoints/self.TargetPoints 在创建 class(继承代码)的实例后初始化。 There is no other section where the lists are manipulated.没有其他部分可以操作列表。 - self.SourcePoints/self.TargetPoints are lists of lists - the code snipped is part of a function within the class - self.SourcePoints/self.TargetPoints 是列表列表 - 截断的代码是 class 中 function 的一部分

I am by far not an expert in programming, so i might oversee something obvious here, but i cant see it and it drives me crazy, because the two print commands dont have the same output...and the script crashes (ofc) because self.SourcePoints/self.TargetPoints are "wrong" in the subsequent iteration.我到目前为止还不是编程专家,所以我可能会在这里监督一些明显的事情,但我看不到它,这让我发疯,因为两个打印命令没有相同的 output ......并且脚本崩溃(ofc)因为self.SourcePoints/self.TargetPoints 在后续迭代中是“错误的”。

So you might say now, by writing所以你现在可以说,写

        SourcePoints = self.SourcePoints

you dont make a copy, but create a reference to the list, so naturally the origin-list gets changed during the for-loops.您不制作副本,而是创建对列表的引用,因此在 for 循环期间自然会更改原始列表。 Makes perfect sense to me, thats why i tried对我来说很有意义,这就是我尝试的原因

        SourcePoints = copy.copy(self.SourcePoints)

and

        SourcePoints = self.SourcePoints.copy()

which both didnt change anything.两者都没有改变任何东西。

Then i said to myself, maybe its because of the identical name of the lists.然后我对自己说,可能是因为列表名称相同。 So the script thinks if i take the variable "SourcePoints" within a class function i want to change the variable self.SourcePoints.所以脚本认为如果我在 class function 中取变量“SourcePoints”,我想更改变量 self.SourcePoints。 So i changed everything to所以我把一切都改成了

        srcPoints = self.SourcePoints.copy()

And it still doesnt work... I have really no idea what is going on and it drives me crazy.它仍然不起作用......我真的不知道发生了什么,它让我发疯。 Fortunately i have a bypass for the script, but i am still trying to figure out why this keeps happening.幸运的是,我绕过了脚本,但我仍在试图弄清楚为什么会一直发生这种情况。 It makes absolutely no sense to my how the two print commands dont have identical output.两个打印命令没有相同的 output 对我来说完全没有意义。 Yet, the computer is just doing what i tell him to do.然而,计算机只是在做我告诉他做的事情。

Can somebody enlighten me?有人可以启发我吗?

Greets, Void问候,空

You're creating shallow copies of the instance variable.您正在创建实例变量的浅表副本。 To get a fully independent copy of an object you can use the copy.deepcopy() function.要获得 object 的完全独立副本,您可以使用copy.deepcopy() function。

Try making the below changes to your first 2 lines尝试对前 2 行进行以下更改

import copy
SourcePoints = copy.deepcopy(self.SourcePoints)
TargetPoints = copy.deepcopy(self.TargetPoints)

You might find this post helpful for more information on shallow and deep copying - Understanding dict.copy() - shallow or deep?您可能会发现这篇文章有助于了解有关浅层和深层复制的更多信息 - 了解 dict.copy() - 浅层还是深层?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM