0
我想通過它的一個屬性對python中的列表進行排序,但是,它沒有正確排序,任何提示。Python排序工作不正常
我嘗試在排序前後打印值,它們打印的值相同。
def kmeans_clustering(cluster_list, num_clusters, num_iterations):
"""
Compute the k-means clustering of a set of clusters
Input: List of clusters, number of clusters, number of iterations
Output: List of clusters whose length is num_clusters
"""
# initialize k-means clusters to be initial clusters with largest populations
for idx in range(len(cluster_list)):
print cluster_list[idx].total_population()
print "sort.."
sorted(cluster_list, key = lambda x: x.total_population(), reverse = True)
for idx in range(len(cluster_list)):
print cluster_list[idx].total_population()
排序退貨該列表已排序,但並未實際變異該對象。嘗試cluster_list = sorted(cluster_list ...並查看是否可行 – JB333 2014-10-04 05:24:54
永遠不要遍歷索引範圍,除非您需要索引用於其他目的.' cluster_list中的集羣更清晰更簡潔 – Borodin 2014-10-04 05:46:52