Поскольку вы делаете суммы, это также можно решить с помощью collections.Counter
:
from collections import Counter
data = [['Dave', 3, 'Red'], ['Dave', 4, 'Red'], ['Dave', 2, 'Blue'], ['Sue', 4, 'Red'], ['Sue', 1, 'Red'], ['Sue', 3, 'Yellow']]
user_days = Counter()
project_user_days = Counter()
for (name, num_days, project) in data:
user_days[name] += num_days
project_user_days[(name, project)] += num_days
derived_data = [
[name, num_days, project, user_days[name], project_user_days[(name, project)]]
for (name, num_days, project) in data
]
import pprint
pprint.pprint(derived_data)
# [['Dave', 3, 'Red', 9, 7],
# ['Dave', 4, 'Red', 9, 7],
# ['Dave', 2, 'Blue', 9, 2],
# ['Sue', 4, 'Red', 8, 5],
# ['Sue', 1, 'Red', 8, 5],
# ['Sue', 3, 'Yellow', 8, 3]]