Hi, I've created tuples of type (String, List[Int]) and want to sum the values in the List[Int] part, i.e. the 2nd element in each tuple.
Here is my list / val input=sc.parallelize(List(("abc",List(1,2,3,4)),("def",List(5,6,7,8))))/ I want to sum up values in the 2nd element of the tuple so that the output is (abc,10) (def, 26) I've tried fold, reduce, foldLeft but with no success in my below code to calculate total: / val valuesForDEF=input.lookup("def") val totalForDEF: Int = valuesForDEF.toList.reduce((x: Int,y: Int)=>x+y) println("THE TOTAL FOR DEF IS" + totalForDEF)/ How do i calculate the total? Another query. What will be the difference between the following tuples when created: / val input=sc.parallelize(List(("abc",List(1,2,3,4)),("def",List(5,6,7,8))))/ / val input=sc.parallelize(List(("abc",(1,2,3,4)),("def",(5,6,7,8))))/ Is there a difference in how (1,2,3,4) and List(1,2,3,4) is handled? -- View this message in context: http://apache-spark-user-list.1001560.n3.nabble.com/Calculate-sum-of-values-in-2nd-element-of-tuple-tp25865.html Sent from the Apache Spark User List mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscr...@spark.apache.org For additional commands, e-mail: user-h...@spark.apache.org