I have an array of arrays, like this...
[[\'Abradolf Lincler\', 4], [\'Abradolf Lincler\', 4], [\'Scary Terry\', 4], [\'Abradolf Lincler\', 4], [\'Scary Terry\',
You can use itertools.groupby
for this:
from itertools import groupby
l = [['Abradolf Lincler', 4], ['Abradolf Lincler', 4], ['Scary Terry', 4], ['Abradolf Lincler', 4], ['Scary Terry', 4], ['Scary Terry', 4], ['Abradolf Lincler', 4], ['Scary Terry', 4], ['Scary Terry', 4], ['Abradolf Lincler', 4], ['Abradolf Lincler', 4], ['Abradolf Lincler', 4], ['Summer Smith', 4], ['Summer Smith', 4], ['Summer Smith', 4], ['Abradolf Lincler', 4], ['Summer Smith', 4], ['Summer Smith', 4]]
res = [[name, sum(s[1] for s in score)] for name, score in groupby(sorted(l, key=lambda x: x[0]), key=lambda x: x[0])]
Which is equivalent to the loop:
res = []
for name, score in groupby(sorted(l, key=lambda x: x[0]), key=lambda x: x[0]):
res.append([name, sum(s[1] for s in score)])
and returns:
>>> res
[['Abradolf Lincler', 32], ['Scary Terry', 20], ['Summer Smith', 20]]