根据下面的输入,我想合并具有相同attrA值的行。合并规则必须遵循attrB值的顺序。
有关简单算法模型的任何想法可以解决这个问题吗?
Input:
attrA;attrB;attrC
15;1;-20 <-- (1) merge this attrC value
15;1;-21 <-- (2) merge this attrC value
15;2;-30 <-- (1) with this attrC value
15;2;-33 <-- (2) with this attrC value
15;2;-31 <-- discarded
10;1;-24 <-- (3) merge this attrC value
10;2;-32 <-- (3) with this attrC value
Output:
(-20,-30);15 <-- (1)
(-21;-33);15 <-- (2)
(-24;-32);10 <-- (3)
另一个输入/输出样本:
Input:
15;1;-24
15;1;-21
15;1;-23
15;2;-30
10;1;-27
Output:
(-24,-30);15
编辑:这是我的尝试。我正在寻找更优化的版本。
output = []
foreach distinct attrA in input
inputFromAttrA = filter(input, attrA) // get only records of current attrA value
r = getRecordCountOfAttrB_thatHasLessRecords(inputFromAttrA)
for i=0 to r; i++
k = 0
attrCmerge = []
foreach distinct attrB in inputFromAttrA
inputFromAttrA_and_AttrB = filter(inputFromAttrA, attrB)
attrCmerge[k] = inputFromAttrA_and_AttrB[i]
k++
output[] = attrCmerge;attrA