2014-03-02 20 views
2

我有Scala元组2列表,我必须将它们组合在一起。我目前使用以下方式来执行它。更好和有效的方式来组合scala中的元组2列表

var matches:List[Tuple2[String,Int]] 
var m = matches.toSeq.groupBy(i=>i._1).map(t=>(t._1,t._2)).toSeq.sortWith(_._2.size>_._2.size).sortWith(_._2.size>_._2.size) 

上述分组给我 序号[(字符串,SEQ [(字符串,整数)])] ,但我想有 序号[(字符串,SEQ [INT])]

我想知道有没有更好的和有效的方法来相同。

+0

预期的行为的一个例子是有帮助的。 – Landei

回答

3

首先,一些想法:

// You should use `val` instead of `var` 
var matches: List[Tuple2[String, Int]] = List("a" -> 1, "a" -> 2, "b" -> 3, "c" -> 4, "c" -> 5) 
var m = matches 
    .toSeq       // This isn't necessary: it's already a Seq 
    .groupBy(i => i._1) 
    .map(t => (t._1, t._2))   // This isn't doing anything at all 
    .toSeq 
    .sortWith(_._2.size > _._2.size) // `sortBy` will reduce redundancy 
    .sortWith(_._2.size > _._2.size) // Not sure why you have this twice since clearly the 
            // second sorting isn't doing anything... 

那么试试这个:

val matches: List[Tuple2[String, Int]] = List("a" -> 1, "a" -> 2, "b" -> 3, "c" -> 4, "c" -> 5) 
val m: Seq[(String, Seq[Int])] = 
    matches 
    .groupBy(_._1) 
    .map { case (k, vs) => k -> vs.map(_._2) } // Drop the String part of the value 
    .toVector 
    .sortBy(_._2.size) 
println(m) // Vector((b,List(3)), (a,List(1, 2)), (c,List(4, 5))) 
+0

谢谢。我只是在寻找这个。特别是在形成整数列表的映射时,字符串部分的丢弃。 – Balaram26

相关问题