在Scala中将List [Map [String,Map [String,Int]]]转换为Map [Int,Int]

用户名

给定以下内容:

val t: List[Map[String, Map[String, Int]]] = List(
  Map("a" -> Map("m" -> 1, "l" -> 21)),
    Map("a" -> Map("m" -> 2, "l" -> 22)),
    Map("a" -> Map("m" -> 3, "l" -> 23)),
    Map("a" -> Map("m" -> 4, "l" -> 24))
)

我想要结果:

Map(1->21,2->22,3->23,4->24)

到目前为止,我有:

val tt = (for {
  (k,v) <- t
  newKey = v("m")
  newVal = v("l")
} yield Map(newKey -> newVal)).flatten.toMap

但这不是类型检查,因此我无法理解为什么不能缺少基本的理解?

我的问题是:

  1. 为什么我的代码有错误?
  2. 我想要进行转换的最惯用的方式是什么?
反应堆僧侣

您有List[Map[...]],不是,Map[...]所以您想先将其拆包。

val tt = (for {
  map <- t
  (k, v) <- map
} ...)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Scala中将Map [String,String]转换为Map [String,Int]

Kotlin FP:将List <String>转换为Map <String,Int>

如何在 Flutter 中将 map 的键从 int 转换为 String

基于 Map 从 List[String] 转换为 List[Int]

从Map [String,List [Int]]折叠为Map [String,Int]

将字符串转换为Map(String,List([Int,Int]))

如何在 dart (Flutter) 中将枚举转换为 Map<int, String>

Scala中的方法签名:Map(String,Int)

Scala Map [String,Int]类投放灾难

Scala将Map [String,Int](“ a”-> 2,“ b”-> 1)转换为序列Seq(“ a”,“ a”,“ b”)

如何将 txt 文件转换为 scala.collection.mutable.Map[String, Int]

将 map<string, int> 转换为 void* 并返回并获取键

颤振:将 int 与 list<Map<String,int>> 进行比较

如何使用scala从文件中读取输入并将文件的数据行转换为List [Map [Int,String]]?

将Array [Int]转换为Map [Int,Int]的Scala功能样式

颤振:类型 'List<Map<String, Object>>' 不是类型转换中类型 'List<Map<String, int>>' 的子类型

模式匹配Map [String,Int]中的Scala类型擦除

Scala-如何将Map [Int,List [List [IntVar]]]转换为

如何在Java 8中将Map <Shape,int []>转换为Map <Shape,Set <Integer >>?

将Seq [A]转换为Map [Int,Seq [A]]

在Kotlin中将Map <String,List <String >>转换为List <Map <String,String >>

如何对Map <String,Map <String,int >> Dart进行排序

将Map <String,Object>转换为Map <String,List <Object >>

外部类型问题的JSON序列化-将map [string] interface {}项转换为int

使用Terraform将列表(map(list(map(string()))))转换为map(list(map(string))))

使用go,如何将map [int] T转换为map [string] T以与JSON一起使用?

如何通过Map [string] int的值排序?

使用map [string] int作为map [interface {}] interface {}类型的参数

如何在Scala中将类型为Map [String,Map [String,Any]]的嵌套映射转换为JSON?