克隆列表似乎充当别名,即使明确声明为克隆
我在使用以下脚本时遇到一些问题。应将以下列表复制 3 份,以便可以独立修改。然而,它似乎创建了同一个列表的 3 个克隆,当你修改一个列表时,你就会修改它们全部。这是该函数:
def calculateProportions(strategies,proportions):
import itertools
combinations = []
columns = list(itertools.product(strategies,repeat=3))
for i in range(0,len(columns)):
columns[i] = list(columns[i])
for n in range(0,len(strategies)):
combinations.append(columns[:])
combinations[0][0][0] = "THIS SHOULD ONLY BE IN ONE PLACE"
print combinations
strategies = [[0,0],[0,50],[50,50]]
calculateProportions(strategies,[])
请注意,当您运行此函数时,您会看到字符串“THIS SHOULD BE IN ONE PLACE”3 次(位置 [0][0][0]、[1][0][0],以及[2][0][0],这似乎是因为这些列表是别名在一起而不是克隆的,但是我在
最后一个小时里一直在研究您的建议。非常感谢解决方案!
I am having some trouble with the following script. It should make 3 copies of the following list so that they can be modified independently. However, it seems to be creating 3 clones of the same list, and when you modify one you modify them all. Here is the function:
def calculateProportions(strategies,proportions):
import itertools
combinations = []
columns = list(itertools.product(strategies,repeat=3))
for i in range(0,len(columns)):
columns[i] = list(columns[i])
for n in range(0,len(strategies)):
combinations.append(columns[:])
combinations[0][0][0] = "THIS SHOULD ONLY BE IN ONE PLACE"
print combinations
strategies = [[0,0],[0,50],[50,50]]
calculateProportions(strategies,[])
Notice how, when you run this, you see the string "THIS SHOULD BE IN ONE PLACE" 3 times (position [0][0][0],[1][0][0], and [2][0][0], not once. This appears to be because the lists are aliased together rather than cloned. However I explicitly cloned it.
I have spent the last hour banging my head into the table on this. Your suggested solutions are much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您克隆
列
时,您仅执行浅拷贝 ,即列表被克隆,但其项目未被克隆,因此在组合
和列
中使用相同的项目引用。您可以使用 copy.deepcopy() 函数来执行对象的深层复制:
或者,更简单地说,列表理解:
You're only performing a shallow copy when you clone
columns
, i.e. the list is cloned but its items are not, so the same item references are used in bothcombinations
andcolumns
.You can use the copy.deepcopy() function to perform a deep copy of the object:
Or, more simply, a list comprehension:
获取像
list[:]
这样的列表副本不会创建列表中包含的元素的副本(即它是平面副本,而不是深层副本)。下面的示例代码说明了这一点:正如 ulidtko 所建议的, copy 模块可能会帮助您案件。
Getting a copy of a list like
list[:]
does not create copies of the elements contained in the list (i.e. it is a flat copy, not a deep copy). The following example code illustrates this:As suggested by ulidtko, the copy module might help in your case.
当你写
l = alist[:]
时,你正在做一个浅拷贝。也就是说,列表不同,但两个列表指向相同的对象。因此,如果您修改列表中的一个元素,则另一个列表中的元素也会被修改。
你需要制作一个深层副本,即。复制列表以及列表中的所有对象。
when you write
l = alist[:]
you're doing a shallow copy. that is to say that the list is different, but the two lists are pointing to the same objects. so if you modify one element of a list, the element in the other list will be modified too.
you need to make a deep copy, ie. copying the list and all the object in the list.
在第一行中,您可以看到函数
copy
和deepcopy
。它们对应于浅复制和深复制。详情请参考http://en.wikipedia.org/wiki/Object_copyIn the very first lines, you can see functions
copy
anddeepcopy
. These correspond to shallow and deep copying. For details, refer to http://en.wikipedia.org/wiki/Object_copy我不会尝试修复深层副本,而是只需使用嵌套列表理解创建所需的数据。这也避免了最终数据的丑陋的手动“积累”。
Instead of trying to fix up the deep copies, I would just create the desired data with nested list comprehensions. This also avoids the ugly manual "accumulation" of the final data.