Yt_Sports 于 2016.04.07 16:24 提问

3个回答

devmiao      2016.04.08 05:40
CSDNXIAON   2016.04.10 16:51

----------------------同志你好，我是CSDN问答机器人小N，奉组织之命为你提供参考答案，编程尚未成功，同志仍需努力！

xringxie   2016.05.14 10:16

• 2010-05-04 12:50 10 10 10
• 2010-05-05 13:50 20 20 20
• 2010-05-06 14:50 30 30 30
• 2010-05-05 13:50 20 20 20
• 2010-05-06 14:50 30 30 30
• 2010-05-04 12:50 10 10 10
• 2010-05-04 11:50 10 10 10

`````` object Demo {

def main(args: Array[String]) {
val input = Source.fromFile("/home/xring/Desktop/data.tsv").getLines().toVector
val grouped = input.map { line =>
val parts = line.split("\t")
(parts.take(2).mkString("\t"), (parts(2).toInt, parts(3).toInt, parts(4).toInt))
}.groupBy(_._1).map(x => (x._1, x._2.map(_._2)))
val res = grouped.map(x => (x._1, x._2.reduce((a, b) => (a._1 + b._1, a._2 + b._2, a._3 + b._3))))
.toVector.sorted
.map(x => x._1 + "\t" + x._2._1 + "\t" + x._2._2 + "\t" + x._2._3)
res.foreach(println)
}

}
``````

• 2010-05-04 11:50 10 10 10
• 2010-05-04 12:50 20 20 20
• 2010-05-05 13:50 40 40 40
• 2010-05-06 14:50 60 60 60