I have the following code:
names = ['a', 'e', 'c', 'd', 'b']
stuff = [['a', 1], ['a', 2], ['b', 5], ['c', 4], ['c', 2], ['d', 10], ['e', 22], ['e', 13]]
x = [[0] * len(stuff)] * len(names)
counters = [0]*len(names)
for s in stuff:
i = names.index(s[0])
x[i][counters[i]] = s
counters[i] += 1
for i in range(len(names)):
if 0 in x[i]:
x[i] = x[i][:x[i].index(0)]
print x
What I would like to have is the following output:
[[['a', 1], ['a', 2]], [['e', 22], ['e', 13]], [['c', 4], ['c', 2]], [['d', 10]], [['b', 5]]]
Essentially, I want to create a new list that contains sublists of sublists where the 0th entry of the sublists at the lowest level is always the same (sorry, I don't know how to put it in a better way). I want to create this new list from the sublists in 'stuff' by using the elements of 'names'. What I get when running my code is:
[[['e', 22], ['e', 13]], [['e', 22], ['e', 13]], [['e', 22], ['e', 13]], [['e', 22], ['e', 13]], [['e', 22], ['e', 13]]]
My question is: why does my code produce this output instead of what i want? I am pretty sure that things go wrong in the line
x[i][counters[i]] = s
However, I have no idea why so any help would really be appreciated...