将按键分组的序列展开为未分组序列的列表,并在Scala中附加键

时间:2019-03-10 20:52:43

标签: scala sequence flatten

我在Scala中具有以下对象:

List[(String,Map[String, Seq[(Int, Double)]])]

我想将其转换为单个行的序列,其中序列的每一行都有4个项:(String,String,Int,Double)

例如,如果我有以下数据:

List(
  ("SuperGroup1", Map("SubGroup1" -> Seq((17,24.1),(38,39.2)))),
  ("SuperGroup1", Map("SubGroup2" -> Seq((135,302.3),(938,887.4))))
)

我想把它变成:

Seq(
  ("SuperGroup1","SubGroup1",17,24.1),
  ("SuperGroup1","SubGroup1",38,39.2),
  ("SuperGroup1","SubGroup2",135,302.3),
  ("SuperGroup1","SubGroup2",938,887.4)
)

我认为您可以使用flatMap或类似的方法,但是我不确定这将如何工作。我看到RDD具有称为flatMapValues的功能,但是对于像我一样的标准列表/地图组合呢?

1 个答案:

答案 0 :(得分:1)

给出您的类型和以下输入:

val input = Seq(
  ("SuperGroup1", Map("SubGroup1" -> Seq(((17,24.1),(38,39.2))))),
  ("SuperGroup1", Map("SubGroup2" -> Seq(((135,302.3),(938,887.4)))))
)

这会将您的输入转换成您期望的形式

input.flatMap { superGroupBox =>
  superGroupBox._2.toSeq.flatMap { subGroupBox =>
    subGroupBox._2.flatMap(x => Seq(x._1, x._2).map(numericTuple => (superGroupBox._1, subGroupBox._1, numericTuple._1, numericTuple._2)))
  }
}