0

I have two lists both of them of the same size I have 28 sets, and I need to count how many elements per set there are, these sets may also be empty, since they are the fruit of a clustering analysis. I was thinking to have a list of lists called cluster_entries such as cluster_entries = [[0]*28], and then appending the corresponding value found ad idx_n[i] to the corresponding cluster_entries[idx_n[i]]

so for instance if I have idx_n[20] = 10 I would like to add to the list 20 of cluster_entries the value 20. Thus I wrote the following code:

for i in range(len(idx_n)):
    print i, idx_n[i]
    cluster_entries[int(idx_n[i])].append(list_from_files[i])

Unfortunately this code appends always to the first element... and I do not understand why

MixturaDementiae
  • 115
  • 1
  • 2
  • 9

1 Answers1

0

Your list

cluster_entries = [[0]*28]

is a list of 28 identical references to the same list. You need to use instead

cluster_entries = [ [0] for i in range(28) ]

to have 28 unique lists.

Also, a more pythonic way of iterating over idx_n is

for i, idx in enumerate(idx_n):
    print i, idx
    cluster_entries[ int(idx) ].append( list_from_files[i] )
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Still does not work, I have tried with cluster_entries = [ [0]*28 ... ] bust still it appends either everything to the same entry in the list or the same value to all the entries in the lists... – MixturaDementiae Jul 12 '13 at 14:58
  • Read my answer again. You *can't* use `[ [0] * 28 ]`. You need to use a list comprehension. – chepner Jul 12 '13 at 15:01
  • sorry I expressed myself in a wrong way I tried exactly what you wrote cluster_entries = [ [0] for i in range(28) ] sorry for the mistake – MixturaDementiae Jul 12 '13 at 15:06
  • How do you set the value of `idx_n`? – chepner Jul 12 '13 at 15:26
  • it is the list generated from a numpy array `idx, _ = vq.vq(data, centroids)` – MixturaDementiae Jul 12 '13 at 15:41
  • fixed! `idx_n = idx.tolist() for index, value in enumerate(idx_n): cluster_entries[ int(value) ].append(list_from_files[index]) cluster_score[ int(value) ] = (cluster_score[ int(value) ]) + (scores_05[ list_from_files[index]])` – MixturaDementiae Jul 12 '13 at 16:54