简体   繁体   English

鉴于字符串列表表示 Scala 中 map 的键,如何将 List[String] 转换为 List[Map[String,String]]?

[英]How do I transform a List[String] to a List[Map[String,String]] given that the list of string represents the keys to the map in Scala?

I have a list of references :我有一个references列表:

val references: List[String]= List("S","R")

I also have variables which is:我也有variables是:

val variables: Map[String,List[String]]=("S"->("a","b"),"R"->("west","east"))

references is a list of keys of the variables map. references是变量 map 的键列表。

I want to construct a function which takes:我想构建一个 function ,它需要:

def expandReplacements(references:List[String],variables:Map[String,List[String]]):List[Map(String,String)]

and this function should basically create return the following combinations而这个 function 应该基本上创建返回以下组合

List(Map("S"->"a"),("R"->"west"),Map("S"->"a"),("R"->"east"),Map("S"->"b"),("R"->"west"),Map("S"->"b"),("R"->"east"))

I tried doing this:我试过这样做:

val variables: Map[String,List[String]] = Map("S" -> List("a", "b"), "R" -> List("east", "central"))
val references: List[String] = List("S","R")

def expandReplacements(references: List[String]): List[Map[String, String]] =
  references match {
    case ref :: refs =>
      val variableValues =
        variables(ref)
      val x = variableValues.flatMap { variableValue =>
        val remaining = expandReplacements(refs)
        remaining.map(rem => rem + (ref -> variableValue))
      }
      x

    case Nil => List.empty
  }

If you have more than 2 references, you can do:如果您有超过 2 个参考,您可以执行以下操作:

def expandReplacements(references: List[String], variables :Map[String,List[String]]): List[Map[String, String]] = {
  references match {
    case Nil => List(Map.empty[String, String])
    case x :: xs =>
      variables.get(x).fold {
        expandReplacements(xs, variables)
      } { variableList =>
        for {
          variable <- variableList.map(x -> _)
          otherReplacements <- expandReplacements(xs, variables)
        } yield otherReplacements + variable
      }
  }
}

Code run at Scastie .代码在Scastie运行。

So I have Figured it Out所以我想通了

def expandSubstitutions(references: List[String]): List[Map[String, String]] =
        references match {
          case r :: Nil => variables(r).map(v => Map(r -> v))
          case r :: rs  => variables(r).flatMap(v => expandSubstitutions(rs).map(expanded => expanded + (r -> v)))
          case Nil      => Nil
        }

This Returns:这返回:

List(Map(R -> west, S -> a), Map(R -> east, S -> a), Map(R -> west, S -> b), Map(R -> east, S -> b))

Your references representation is suboptimal but if you want to use it...您的参考表示不是最理想的,但如果您想使用它...

val variables: Map[String,List[String]] = [S -> List("a", "b"), R -> List("east", "central")]
val references: List[String] = List("S","R")

def expandReplacements(references: List[String]): List[Map[String, String]] =
  references match {
    case List(aKey, bKey) =>
      val as = variables.get(aKey).toList.flatten
      val bs = variables.get(bKey).toList.flatten

      as.zip(bs).map { case (a, b) =>
        Map(aKey -> a, bKey -> b)
      }
    case _ => Nil
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Scala将Map [String,List [String]]转换为Map [String,String] - Scala Convert a Map[String, List[String]] to a Map[String, String] 在 Scala 中将 List[(String, String)] 转换为 List[Map[String, String]] - Convert a List[(String, String)] to List[Map[String, String]] in scala 如何转换列表<String>到地图<String,String>使用 Google 收藏? - How to transform List<String> to Map<String,String> with Google collections? 如何改造一个Map <string, list<string> > 进入列表<map<string, string> > 得到笛卡尔积? </map<string,></string,> - How to transform a Map<String, List<String>> into a List<Map<String, String>> and get Cartesian product? Scala:列出[Tuple3]到Map [String,String] - Scala: List[Tuple3] to Map[String,String] 如何翻转列表<Map<String, String> &gt; 具有与 Map 相似的键<String,List<String> &gt;? - How to turn a List<Map<String, String>> with similar keys to a Map<String,List<String>>? 如何将 json 字符串解析为 scala 中的 Map 列表? - How to parse a json string to a List of Map in scala? 列表/映射中的列表和字符串 - List and String in list/map 列表<map<string, object> &gt; 至 Map <string, string[]></string,></map<string,> - List<Map<String, Object>> to Map<String, String[]> 如何转换的地图 <String, List<Object> &gt;进入 <Object, List<String> &gt;? - How do I convert a Map of <String, List<Object>> into a Map of <Object, List<String>>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM