Here is an example using itertools.groupby() in Python:
from itertools import groupby Positions = [ ('AU', '1M', 1000), ('NZ', '1M', 1000), ('AU', '2M', 4000), ('AU', 'O/N', 4500), ('US', '1M', 2500), ] FLD_COUNTRY = 0 FLD_CONSIDERATION = 2 Pos = sorted(Positions, key=lambda x: x[FLD_COUNTRY]) for country, pos in groupby(Pos, lambda x: x[FLD_COUNTRY]): print country, sum(p[FLD_CONSIDERATION] for p in pos)
Is there any language construct or library support in Java that behaves or can achieve what itertools.groupby() does above?
source share