I'm trying to find a way to get a final running average of elements in a nested list using a for loop and I'm getting stuck on how to go about it. I have some sample data below, where the element[0] of each sublist is an id marker to be grouped, element[1] is the value to be summed, and element[2] is acting as a counter
listA = [[u001,4,1],[u002,6,1],[u003,3,1],[u001,12,1],[u002,1,1]
listB = []
for data in listA:
if data[0] not in listB:
listB.append(data)
else: # essentailly if the value is in listB then do:
listB[1] += data[1] #but this won't work because listB[1] is a list, and data[1] is a value
# i need to find a way to append the value data in listB WHERE listB[0] == data[0]
AND
listB[2] += 1 #at that specific location
eg pf each for loop pass:
listA:[[u001,4,1],[u002,6,1],[u003,3,1],[u001,12,1],[u002,1,1]
listB:[]
listB:[[u001,4,1]]
listB:[[u001,4,1],[u002,6,1]]
listB:[[u001,4,1],[u002,6,1],[u003,3,1]]
listB:[[u001,16,2],[u002,6,1],[u003,3,1]]
listB:[[u001,16,2],[u002,7,2],[u003,3,1]] #final desired result