如何从 csv 字符串获取地图

发布于 2024-09-09 06:50:59 字数 733 浏览 1 评论 0原文

我对 Scala 相当陌生,但我现在正在做练习。
我有一个像这样的字符串

"A>Augsburg;B>Berlin"
. What I want at the end is a map

val mymap = Map("A"->"Augsburg", "B"->"Berlin")

我所做的是:

val st = locations.split(";").map(dynamicListExtract _)
with the function
private def dynamicListExtract(input: String)  = {
    if (input contains ">") {
      val split = input split ">"
      Some(split(0), split(1)) // return key , value
    } else {
      None 
    } 
  }
Now I have an
Array[Option[(String, String)
How do I elegantly convert this into a Map[String, String]

有人可以帮忙吗? 谢谢

I'm fairly new to Scala, but I'm doing my exercises now.
I have a string like

"A>Augsburg;B>Berlin"

. What I want at the end is a map

val mymap = Map("A"->"Augsburg", "B"->"Berlin")

What I did is:

val st = locations.split(";").map(dynamicListExtract _)


with the function

private def dynamicListExtract(input: String)  = {
    if (input contains ">") {
      val split = input split ">"
      Some(split(0), split(1)) // return key , value
    } else {
      None 
    } 
  }


Now I have an

Array[Option[(String, String)


How do I elegantly convert this into a Map[String, String]

Can anybody help?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

墨落成白 2024-09-16 06:50:59

只需将 map 调用更改为 flatMap

scala> sPairs.split(";").flatMap(dynamicListExtract _)
res1: Array[(java.lang.String, java.lang.String)] = Array((A,Augsburg), (B,Berlin))

scala> Map(sPairs.split(";").flatMap(dynamicListExtract _): _*)
res2: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map((A,Augsburg), (B,Berlin))

作为比较:

scala> Map("A" -> "Augsburg", "B" -> "Berlin")
res3: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map((A,Augsburg), (B,Berlin))

Just change your map call to flatMap:

scala> sPairs.split(";").flatMap(dynamicListExtract _)
res1: Array[(java.lang.String, java.lang.String)] = Array((A,Augsburg), (B,Berlin))

scala> Map(sPairs.split(";").flatMap(dynamicListExtract _): _*)
res2: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map((A,Augsburg), (B,Berlin))

For comparison:

scala> Map("A" -> "Augsburg", "B" -> "Berlin")
res3: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map((A,Augsburg), (B,Berlin))
凡间太子 2024-09-16 06:50:59

在 2.8 中,您可以执行以下操作:

val locations = "A>Augsburg;B>Berlin"
val result = locations.split(";").map(_ split ">") collect { case Array(k, v) => (k, v) } toMap

collect 类似于 map,但也会过滤部分函数中未定义的值。 toMap 将从 Traversable 创建一个 Map,只要它是 Traversable[(K, V)]

In 2.8, you can do this:

val locations = "A>Augsburg;B>Berlin"
val result = locations.split(";").map(_ split ">") collect { case Array(k, v) => (k, v) } toMap

collect is like map but also filters values that aren't defined in the partial function. toMap will create a Map from a Traversable as long as it's a Traversable[(K, V)].

谁的新欢旧爱 2024-09-16 06:50:59

Randall 的 for-compression 形式的解决方案也值得一看,它可能更清晰,或者至少让您更好地了解 flatMap 正在做什么。

Map.empty ++ (for(possiblePair<-sPairs.split(";"); pair<-dynamicListExtract(possiblePair)) yield pair)

It's also worth seeing Randall's solution in for-comprehension form, which might be clearer, or at least give you a better idea of what flatMap is doing.

Map.empty ++ (for(possiblePair<-sPairs.split(";"); pair<-dynamicListExtract(possiblePair)) yield pair)
遮了一弯 2024-09-16 06:50:59

一个简单的解决方案(不处理错误情况):

val str = "A>Aus;B>Ber"
var map = Map[String,String]()

str.split(";").map(_.split(">")).foreach(a=>map += a(0) -> a(1))

但 Ben Lings 的更好。

A simple solution (not handling error cases):

val str = "A>Aus;B>Ber"
var map = Map[String,String]()

str.split(";").map(_.split(">")).foreach(a=>map += a(0) -> a(1))

but Ben Lings' is better.

撩起发的微风 2024-09-16 06:50:59
val str= "A>Augsburg;B>Berlin"

Map(str.split(";").map(_ split ">").map(s => (s(0),s(1))):_*)

--or--

str.split(";").map(_ split ">").foldLeft(Map[String,String]())((m,s) => m + (s(0) -> s(1)))
val str= "A>Augsburg;B>Berlin"

Map(str.split(";").map(_ split ">").map(s => (s(0),s(1))):_*)

--or--

str.split(";").map(_ split ">").foldLeft(Map[String,String]())((m,s) => m + (s(0) -> s(1)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文