Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的列表等式_Python_Python 3.x_List - Fatal编程技术网

Python中的列表等式

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],

在此处创建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], [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)