itertools.groupby
, .items()
, itertools.groupby
key
( key,value
). sum()
, , . -
from itertools import groupby
for key,group in groupby(sorted(D_grpTagReport.items()),key=lambda x:x[0][0]):
total = sum(g[1] for g in group)
print(key,':',total)
, , , dictionary
.
-
>>> D_grpTagReport = { ('Tag1', '1'):345.56 ,
... ('Tag1', '2'):45.6 ,
... ('Tag2', '3'):3.1 ,
... ('Tag3', '1'):21.56 ,
... ('Tag2', '3'):1.56 ,
... }
>>>
>>>
>>> from itertools import groupby
>>> for key,group in groupby(sorted(D_grpTagReport.items()),key=lambda x:x[0][0]):
... total = sum(g[1] for g in group)
... print(key,':',total)
...
Tag1 : 391.16
Tag2 : 1.56
Tag3 : 21.56