Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Aggregate by Operation by kazuki.h
from collections import defaultdict
def aggr_operation(data):
d = defaultdict(int)
for s, n in data:
sing, key = s[0], s[1:]
if sing == "+": d[key] += n
elif sing == "-": d[key] -= n
elif sing == "*": d[key] *= n
elif sing == "/" and n != 0: d[key] /= n
elif sing == "=": d[key] = n
return {k: v for k, v in d.items() if k and v}
Aug. 21, 2022
Comments: