answer = []
for empId, entries in itertools.groupby(sorted(list_pairs, key=operator.itemgetter(0)), key=operator.itemgetter(0)):
answer.append((empId, sum(entry[1] for entry in entries)))
In [17]: list_pairs = [(u'1046', 1.0), (u'8008', 2.3), (u'8008', 2.2), (u'8011', 1.3)]
In [18]: answer = []
In [19]: for empId, entries in itertools.groupby(sorted(list_pairs, key=operator.itemgetter(0)), key=operator.itemgetter(0)):
....: answer.append((empId, sum(entry[1] for entry in entries)))
....:
In [20]: answer
Out[20]: [(u'1046', 1.0), (u'8008', 4.5), (u'8011', 1.3)]
これを読みやすくするには:
answer = []
list_pairs.sort(key=operator.itemgetter(0))
groups = itertools.groupby(list_pairs, key=operator.itemgetter(0))
for empId, entries in groups:
answer.append((empId, sum(entry[1] for entry in entries)))