How to sum a list of tuples

前端 未结 5 1365
囚心锁ツ
囚心锁ツ 2021-01-02 04:06

Given the following list of tuples...

val list = List((1, 2), (1, 2), (1, 2))

... how do I sum all the values and obtain a single tuple lik

5条回答
  •  再見小時候
    2021-01-02 04:47

    answering to this question while trying to understand aggregate function in spark

    scala> val list = List((1, 2), (1, 2), (1, 2))
    list: List[(Int, Int)] = List((1,2), (1,2), (1,2))
    
       scala>  list.aggregate((0,0))((x,y)=>((y._1+x._1),(x._2+y._2)),(x,y)=>(x._1+y._2,y._2+x._2))
    res89: (Int, Int) = (3,6)
    

    Here is the link to the SO QA that helped to understand and answer this [Explain the aggregate functionality in Spark

提交回复
热议问题