Python中的列表等式
在此处创建KMeans群集算法: 陷入列表平等Python中的列表等式,python,python-3.x,list,Python,Python 3.x,List,在此处创建KMeans群集算法: 陷入列表平等 #clusters = [[],[]] prevclusters = list(clusters) # Making a new list using clusters elements. ..... clusters[loc].append(inputs[i]) # Modifying clusters in a for loop .... # Now, clusters = [[[1, 1], [1, 2]], [[3, 7], [4, 5],
#clusters = [[],[]]
prevclusters = list(clusters) # Making a new list using clusters elements.
.....
clusters[loc].append(inputs[i]) # Modifying clusters in a for loop
....
# Now, clusters = [[[1, 1], [1, 2]], [[3, 7], [4, 5], [5, 5]]]
if prevclusters == clusters: # Gives True, why ?
在
集群
中的loc
处编辑项目时,两个列表仍然引用已修改的相同子列表。创建prevclusters
时,您可能需要复制。deepcopy
列表:
from copy import deepcopy
prevclusters = deepcopy(clusters)
在
集群
中的loc
处编辑项目时,两个列表仍然引用已修改的相同子列表。创建prevclusters
时,您可能需要复制。deepcopy
列表:
from copy import deepcopy
prevclusters = deepcopy(clusters)
要查看相同的效果,请尝试
L=[[]]*4;印刷品(L);L[0]。追加(1);打印(L)
要查看相同的效果,请尝试L=[[]]*4;印刷品(L);L[0]。追加(1);打印(L)