【问题标题】:python add list into a list error [duplicate]python将列表添加到列表错误[重复]
【发布时间】:2023-04-04 02:48:01
【问题描述】:

编写python代码将列表添加到列表中,如下:

mbk = MiniBatchKMeans(n_clusters=cluster_number).fit(normal_one_dataset_matrix)
labels = mbk.labels_

miniBatchKMeans_cluster_centers = mbk.cluster_centers_

center_list = []
center_list[0] = miniBatchKMeans_cluster_centers

miniBatchKMeans_cluster_centers 是这样的:

[[  1.35976987e-01   3.62686053e-02   1.57226220e-02   1.55101081e-01
    9.22227057e-02   4.69291492e-03   6.31832469e-01   1.18909881e-01
    3.98820502e-02   1.31951911e-01   7.74235425e-02]
 [  6.77025243e-02   4.98708544e-02   1.35180194e-02   1.87703505e-01
    8.38242136e-02   5.39580583e-03   2.70955680e-01   2.70501184e-01
    6.28647087e-02   2.05018029e-01   1.90661340e-01]
 [  2.28943182e-02   1.18150568e-02   3.38147727e-03   3.76551136e-02
    3.63664773e-03   3.86363636e-04   5.12753977e-02   3.60461932e-02
    0.00000000e+00   8.47871989e-01   6.48064205e-02]]

运行时报错:

File "F:/MyDocument/F/My Document/Training/Python/PyCharmProject/FaceBookCrawl/FB_group_user_stability.py", line 47, in minibatchkmeansClustering_no_gender
center_list[0] = miniBatchKMeans_cluster_centers
IndexError: list assignment index out of range

请告诉我原因和解决方法

【问题讨论】:

  • center_list 为空。您不能索引空列表。也许您可以将append 转换为center_list,但我不确定您要达到的目标。

标签:
python