Making a KMeans clustering algorithm here: Getting stuck on list equality
#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 ?
When editing the item at loc
in cluster
, both lists still reference the same sublist, which has been modified. You may want to copy.deepcopy
the list when creating prevclusters
:
from copy import deepcopy
prevclusters = deepcopy(clusters)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.