给定一个包含(key, (index, rating))
形式条目的数组,例如:
val samp = Array((0, (1, 0.005)), (1, (1, 0.306)), (0, (0, 0.231)), (2, (2, 0.908)), (0, (2, 0.542)), (2, (1, 0.091)))
我想将其转换为
Array((key1, Array((index1, rating1), (index2, rating2), ...), (key2, Array((index1, rating1), (index2, rating2), ...)), ...)
为了得到这个结果,我做了以下代码:
samp.groupBy(_._1).map{ case (k, v) => (k, v.map(_._2)) }.toArray
给了我:
Array((2,Array((2,0.908), (1,0.091))), (1,Array((1,0.306))), (0,Array((1,0.005), (0,0.231), (2,0.542))))
代码给了我想要的东西,但我只是想知道是否有更好的方法可以做到这一点,因为我对scala很新。谢谢!