I guess, list is an object, so this behavior must be due to its reference being appended, and so all the elements get updated! How does this work exactly and what's the right way to do this?
Code (to increment index position discovered):
# generating labels for debugging
#labels = [3]*3 + [1]*3 + [4]*2
labels = [3, 3, 3, 1, 1, 1, 4, 4]
print "labels list is", labels
counter_table = []
#counter = [0]*6
counter = [0 for i in range(6)]
for i in labels:
curr_label = i
counter[curr_label] = counter[curr_label] + 1
print "current counter list is", counter
counter_table.append(counter)
print "final counter_table is", counter_table
Output:
labels list is [3, 3, 3, 1, 1, 1, 4, 4]
current counter list is [0, 0, 0, 1, 0, 0]
current counter list is [0, 0, 0, 2, 0, 0]
current counter list is [0, 0, 0, 3, 0, 0]
current counter list is [0, 1, 0, 3, 0, 0]
current counter list is [0, 2, 0, 3, 0, 0]
current counter list is [0, 3, 0, 3, 0, 0]
current counter list is [0, 3, 0, 3, 1, 0]
current counter list is [0, 3, 0, 3, 2, 0]
final counter_table is [[0, 3, 0, 3, 2, 0], [0, 3, 0, 3, 2, 0], [0, 3, 0, 3, 2, 0], [0, 3, 0, 3, 2, 0], [0, 3, 0, 3, 2, 0], [0, 3, 0, 3, 2, 0], [0, 3, 0, 3, 2, 0], [0, 3, 0, 3, 2, 0]]