5

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?

user1243091
  • 175
  • 3
  • 11

3 Answers3

6

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.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
3

If you can just ignore the middle entry, then:

val l = List(('a,'e,1), ('b,'f,2), ('a,'g,3), ('b,'h,4))
l.groupBy(_._1).mapValues(_.map(_._3).sum) 
// Map('b -> 6, 'a -> 4)

If you have to keep the middle entry around:

l.groupBy(_._1).map { 
  case (_, values) =>
    val (a,b,_) = values.head
    (a, b, values.map(_._3).sum)
} 
// List(('b,'f,6), ('a,'e,4))
dhg
  • 52,383
  • 8
  • 123
  • 144
0

You could use the concept of a monoid. If the first two values of your entries build the key values and the remaining the associate value itself, you could use a Map.

Once you have a Map you may proceed like this: Best way to merge two maps and sum the values of same key?

Community
  • 1
  • 1
AndreasScheinert
  • 1,918
  • 12
  • 18