I'm trying to find an efficient way to do the following:
I have this sample:
sample = [['no',2, 6], ['ja',5,7], ['no',4,9], ['ja',10,11], ['ap',7,12]]
and would need
res = [['no', 6, 15], ['ja', 15, 18], ['ap',7,12]]
i.e. sum the corresponding values of the sublists where the first element is the same.
Thanks a lot
My code is:
codes = list(set([element[0] for element in sample]))
res=[]
for code in codes:
aux=[code]
res01 = 0
res02 = 0
for element in sample:
if element[0] == code:
res01 += element[1]
res02 += element[2]
aux += [res01, res02]
res.append(aux)
Using
defaultdict
:#driver values :
Since the output is structured as such, I would suggest you utilise the
dict
type for storing your output as future processing with it will be easier.In case you still want the output as a
list
, just map thedict
, as follows: