Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将列表(List[String])转换为Map[String,Int]?_List_Scala_Map - Fatal编程技术网

如何将列表(List[String])转换为Map[String,Int]?

如何将列表(List[String])转换为Map[String,Int]?,list,scala,map,List,Scala,Map,我有一个列表(列表(“aba,4”),列表(“baa,2”),我想把它转换成地图: val map : Map[String, Int] = Map("aba" -> 4, "baa" -> 2) 最好的归档方式是什么 更新: 我执行数据库查询以检索数据: val(u,myData)=DB.runQuery(…) 这会返回一对,但我只对第二部分感兴趣,它给出了: myData: List[List[String]] = List(List(Hello, 19), List(Worl

我有一个
列表(列表(“aba,4”),列表(“baa,2”)
,我想把它转换成地图:

val map : Map[String, Int] = Map("aba" -> 4, "baa" -> 2)
最好的归档方式是什么

更新:

我执行数据库查询以检索数据: val(u,myData)=DB.runQuery(…)

这会返回一对,但我只对第二部分感兴趣,它给出了:

myData: List[List[String]] = List(List(Hello, 19), List(World, 14), List(Foo, 13), List(Bar, 13), List(Bar, 12), List(Baz, 12), List(Baz, 11), ...)
我的看法:

List(List("aba, 4"), List("baa, 2")) map {_.head} map {itemList => itemList split ",\\s*"} map {itemArr => (itemArr(0), itemArr(1).toInt)} toMap
分步骤:

List(List("aba, 4"), List("baa, 2")).
  map(_.head).                                    //List("aba, 4", "baa, 2")
  map(itemList => itemList split ",\\s*").        //List(Array("aba", "4"), Array("baa", "2"))
  map(itemArr => (itemArr(0), itemArr(1).toInt)). //List(("aba", 4), ("baa", 2))
  toMap                                           //Map("aba" -> 4, "baa" -> 2)
您的输入数据结构有点笨拙,因此我认为您无法进一步优化/缩短它。

我的看法:

List(List("aba, 4"), List("baa, 2")) map {_.head} map {itemList => itemList split ",\\s*"} map {itemArr => (itemArr(0), itemArr(1).toInt)} toMap
List(List("aba, 4"), List("baa, 2")).
  flatten.     //get rid of those weird inner Lists
  map {s=> 
    //split into key and value
    //Array extractor guarantees we get exactly 2 matches
    val Array(k,v) = s.split(","); 
    //make a tuple out of the splits
    (k, v.trim.toInt)}.
  toMap  // turns an collection of tuples into a map
分步骤:

List(List("aba, 4"), List("baa, 2")).
  map(_.head).                                    //List("aba, 4", "baa, 2")
  map(itemList => itemList split ",\\s*").        //List(Array("aba", "4"), Array("baa", "2"))
  map(itemArr => (itemArr(0), itemArr(1).toInt)). //List(("aba", 4), ("baa", 2))
  toMap                                           //Map("aba" -> 4, "baa" -> 2)
您的输入数据结构有点笨拙,因此我认为您无法进一步优化/缩短它。

还有一个例子:

List(List("aba, 4"), List("baa, 2")).
  flatten.     //get rid of those weird inner Lists
  map {s=> 
    //split into key and value
    //Array extractor guarantees we get exactly 2 matches
    val Array(k,v) = s.split(","); 
    //make a tuple out of the splits
    (k, v.trim.toInt)}.
  toMap  // turns an collection of tuples into a map
List(List("aba, 4"), List("baa, 2")).
  flatten.par.collect(
    _.split(",").toList match {
      case k :: v :: Nil => (k, v.trim.toInt) 
  }).toMap
与其他答案的区别:

  • 使用
    .par
    并行创建对,这使我们能够从多个核中获益
  • 使用
    collect
    PartialFunction
    忽略非“key,value”形式的字符串
编辑:
.par
与前面的应答状态相同。列表处理的执行顺序没有保证,因此函数应该没有副作用(或者副作用不应该关心顺序)。

还有另一个注意事项:

List(List("aba, 4"), List("baa, 2")).
  flatten.par.collect(
    _.split(",").toList match {
      case k :: v :: Nil => (k, v.trim.toInt) 
  }).toMap
与其他答案的区别:

  • 使用
    .par
    并行创建对,这使我们能够从多个核中获益
  • 使用
    collect
    PartialFunction
    忽略非“key,value”形式的字符串


编辑:
.par
与前面的应答状态相同。列表处理的执行顺序没有保证,因此函数应该没有副作用(或者副作用不应该关心顺序)。

它与提供的小列表一起工作。然而,当我尝试使用现有的数据列表时,我得到了一个java.lang.ArrayIndexOutOfBoundsException:1。。。我能有所防范吗?@user1243091:列表的大小在这里并不重要。我猜问题是
(itemArr(0),itemArr(1).toInt)
,这意味着一些输入字符串没有遵循
“string,int”
模式。尝试在您的输入上运行此命令:
List(List(“aba,4”)、List(“baa,2”))map{uu.head}map{itemList=>itemList split”、\\s*“}filterNot{uu.size==2}
-它不应该返回任何东西,实际上返回我列表中的所有东西[List[String]]。这很有趣。。。我不明白。我的列表[String]中的条目看起来很像提供的示例数据……它与提供的小列表一起工作。然而,当我尝试使用现有的数据列表时,我得到了一个java.lang.ArrayIndexOutOfBoundsException:1。。。我能有所防范吗?@user1243091:列表的大小在这里并不重要。我猜问题是
(itemArr(0),itemArr(1).toInt)
,这意味着一些输入字符串没有遵循
“string,int”
模式。尝试在您的输入上运行此命令:
List(List(“aba,4”)、List(“baa,2”))map{uu.head}map{itemList=>itemList split”、\\s*“}filterNot{uu.size==2}
-它不应该返回任何东西,实际上返回我列表中的所有东西[List[String]]。这很有趣。。。我不明白。我的列表[String]中的条目看起来确实像提供的示例数据……这给了我一个scala.MatchError:[Ljava.lang.String;@6bf4d04a(属于[Ljava.lang.String;)的$anonfun$1.apply(:23)的$anonfun$1.apply(:20)给我们您正在使用的数据这给了我一个scala.MatchError:[Ljava.lang.String;@6bf4d04a(属于[Ljava.lang.String;)在$anonfun$1.apply(:23)在$anonfun$1.apply(:20)给我们你正在使用的数据一个小提示:
.par
仅在2.9+版本的Scala中可用。(你确定,
.par
会破坏订单吗?)谢谢你发现这一点。我搞砸了中间处理是非有序的(f.ex.
println
调用),但结果值将以相同的顺序再次拼凑在一起。请注意:
.par
仅在2.9+版本的Scala中可用。(您确定,
.par
会破坏顺序吗?)感谢您发现了这一点。我搞砸了,中间处理是非顺序的(f.ex.
println
调用),但结果值将再次以相同的顺序拼凑在一起。+1,各位,这是一个非常非常好的答案。不过,有一件事需要“k->v.toInt”来匹配Map[String,Int]如question@virtualeyes,哦,是的,我没有看到关于
Int
类型的内容。按照建议进行了修复。+1,各位,这是一个非常非常好的答案。不过,有一件事需要“k->v.toInt”来匹配question@virtualeyes,哦,是的,我没有看到关于
Int
类型的问题。已按建议修复。