unpivot a map/list/tuples 使用 scala
unpivot a map/list/tuples using scala
我有这样的东西:
val m1 = Map(A -> List(("a","b"),("c","d"),("e","f")))
我希望结果是:
(Map(A -> List("a","b")), Map(A -> List ("c","d")), Map(A -> List("e","f")))
有人能帮忙吗?
谢谢
很难说出你到底想做什么,但这里有一种方法可以将 m1
转换为所需的结构:
val m1 = Map("A" -> List(("a","b"),("c","d"),("e","f")))
m1.toList.flatMap { case (key, tuple) =>
tuple.map(v => Map(key -> List(v._1, v._2)))
}
我有这样的东西:
val m1 = Map(A -> List(("a","b"),("c","d"),("e","f")))
我希望结果是:
(Map(A -> List("a","b")), Map(A -> List ("c","d")), Map(A -> List("e","f")))
有人能帮忙吗?
谢谢
很难说出你到底想做什么,但这里有一种方法可以将 m1
转换为所需的结构:
val m1 = Map("A" -> List(("a","b"),("c","d"),("e","f")))
m1.toList.flatMap { case (key, tuple) =>
tuple.map(v => Map(key -> List(v._1, v._2)))
}