How to sum values from dict where values are string. I mean how to sum values where multiply key is same in dictionary.
values = [
{
Specific solution, with no attempt at cleverness:
def regroup(values):
groups = dict()
for d in values:
key = (d["prod_name"], d["prod_hola"])
if key in groups:
group = groups[key]
group["rashod"] += d["rashod"]
group["prihod"] += d["prihod"]
else:
groups[key] = d.copy()
return list(groups.values())
And a slightly more generic solution:
def generic_regroup(values, keys):
groups = dict()
valkeys = [k for k in values[0] if k not in key]
for d in values:
key = tuple(d[k] for k in keys)
if key in groups:
group = groups[key]
for k in valkeys:
group[k] += d[k]
else:
groups[key] = d.copy()
return list(groups.values())
results = generic_regroup(values, ("prod_name", "prod_hola"))
Now someone will certainly chime in with a much more clever solution involving itertools...