How to sum up based on a Tuples first elem?
scala, tuples
Solution
Try this:
list groupBy {_._1} mapValues {v => (v.head._1, v.head._2, v map {_._3} sum)}
The middle entry is preserved and it always takes the first one that appeared in the input list.
Problem
I have a 3-tuple list like the following [I added line breaks for readability]: ``` (2, 127, 3) (12156, 127, 3) (4409, 127, 2) <-- 4409 occurs 2x (1312, 127, 12) <-- 1312 occurs 3x (4409, 128, 1) <-- (12864, 128, 1) (1312, 128, 1) <-- (2664, 128, 2) (12865, 129, 1) (183, 129, 1) (12866, 129, 2) (1312, 129, 10) <-- ``` I want to sum up based on the first entry. The first entry should be unique. The result should look like this: ``` (2, 127, 3) (12156, 127, 3) (4409, 127, 3) <- new sum = 3 (1312, 127, 23) <- new sum = 23 (12864, 128, 1) (2664, 128, 2) (12865, 129, 1) (183, 129, 1) (12866, 129, 2) ``` How can I achieve this in Scala?