你可以做一个词典的计算的总数,然后将其转换回一名单:
data = [['c', 2], ['a', 1], ['b', 1], ['a', 2], ['a', 2], ['c', 3]]
totals = {} # could use collections.defaultdict for this part
for k, v in data:
totals[k] = totals.get(k, 0) + v
print(totals) # {'c': 5, 'a': 5, 'b': 1}
output = [[k, v] for k, v in totals.items()]
# or, output = list(map(list, totals.items()))
# or, output = list(totals.items()) # if you are fine with a list of tuples
print(output) # [['c', 5], ['a', 5], ['b', 1]]
# if you want the output to be sorted alphabetically
output = sorted(output, key=lambda lst: lst[0])
print(output) # [['a', 5], ['b', 1], ['c', 5]]