在Python中列出等式

2024-04-25 23:21:52 发布

您现在位置:Python中文网/ 问答频道 /正文

在此处生成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 ?

Tags: 算法new聚类elementsloclistclustersusing
1条回答
网友
1楼 · 发布于 2024-04-25 23:21:52

cluster中编辑loc处的项时,两个列表仍然引用已修改的同一个子列表。创建prevclusters时,可能需要copy.deepcopy列表:

from copy import deepcopy

prevclusters = deepcopy(clusters)

相关问题 更多 >