您将如何测试一组给定数字的所有可能的添加组合,以便它们加起来给定的最终数字?
例:
以下是itertools.groupby()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)
# -> AU 9500
# -> NZ 1000
# -> US 2500
Run Code Online (Sandbox Code Playgroud)
Java中是否有任何语言构造或库支持可以实现或者可以实现itertools.groupby()上述功能?