这平均了地图中包含的值"映射"
case class Point(label: String, points: List[Double])
val mapped = Map(Point("A4", List(5.0, 8.0)) -> List(Point("A3", List(8.0, 4.0)), Point("A4", List(5.0, 8.0))))
//> mapped : scala.collection.immutable.Map[general.Point,List[general.Point]]
//| = Map(Point(A4,List(5.0, 8.0)) -> List(Point(A3,List(8.0, 4.0)), Point(A4,Li
//| st(5.0, 8.0))))
val averaged = mapped.values.map(m => m.map(m2 => m2.points).transpose.map(xs => xs.sum / xs.size))
//> averaged : Iterable[List[Double]] = List(List(6.5, 6.0))
使用转置会导致标签信息丢失。如何对值进行平均,同时保留标签,以便产生类似的结果:
List(List(6.5, 6.0) , List(A3,A4))
而不是:
List(List(6.5, 6.0))
答案 0 :(得分:0)
说实话,我真的不明白你想做什么,但这是一个解决方案:
mapped.values.map(p => (p.map(_.points).transpose.map(xs => xs.sum / xs.size),
p.map(_.label)))