string Scala 将字符串拆分为元组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14971901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:48:05  来源:igfitidea点击:

Scala split string to tuple

stringlistscalasplittuples

提问by princess of persia

I would like to split a string on whitespace that has 4 elements:

我想在具有 4 个元素的空白处拆分字符串:

1 1 4.57 0.83

and I am trying to convert into List[(String,String,Point)] such that first two splits are first two elements in the list and the last two is Point. I am doing the following but it doesn't seem to work:

并且我正在尝试转换为 List[(String,String,Point)] 以便前两个拆分是列表中的前两个元素,后两个是 Point。我正在执行以下操作,但似乎不起作用:

Source.fromFile(filename).getLines.map(string => { 
            val split = string.split(" ")
            (split(0), split(1), split(2))
        }).map{t => List(t._1, t._2, t._3)}.toIterator

采纳答案by Denis Tulskiy

You could use pattern matching to extract what you need from the array:

您可以使用模式匹配从数组中提取您需要的内容:

    case class Point(pts: Seq[Double])
    val lines = List("1 1 4.34 2.34")

    val coords = lines.collect(_.split("\s+") match {
      case Array(s1, s2, points @ _*) => (s1, s2, Point(points.map(_.toDouble)))
    })

回答by Randall Schulz

How about this:

这个怎么样:

scala> case class Point(x: Double, y: Double)
defined class Point

scala> s43.split("\s+") match { case Array(i, j, x, y) => (i.toInt, j.toInt, Point(x.toDouble, y.toDouble)) }
res00: (Int, Int, Point) = (1,1,Point(4.57,0.83))

回答by Ryan K

case class Point(pts: Seq[Double])
val lines = "1 1 4.34 2.34"

val splitLines = lines.split("\s+") match {
  case Array(s1, s2, points @ _*) => (s1, s2, Point(points.map(_.toDouble)))
}

And for the curious, the @ in pattern matching binds a variable to the pattern, so points @ _*is binding the variable points to the pattern *_ And *_ matches the rest of the array, so points ends up being a Seq[String].

奇怪的是,模式匹配中的@ 将变量绑定到模式,因此points @ _*将变量点绑定到模式 *_ 并且 *_ 匹配数组的其余部分,因此点最终成为 Seq[String]。

回答by cheeken

You are not converting the third and fourth tokens into a Point, nor are you converting the lines into a List. Also, you are not rendering each element as a Tuple3, but as a List.

您没有将第三个和第四个标记转换为 a Point,也没有将行转换为 a List。此外,您不是将每个元素呈现为 a Tuple3,而是呈现为 a List

The following should be more in line with what you are looking for.

以下应该更符合您正在寻找的内容。

case class Point(x: Double, y: Double) // Simple point class
Source.fromFile(filename).getLines.map(line => { 
    val tokens = line.split("""\s+""") // Use a regex to avoid empty tokens
    (tokens(0), tokens(1), Point(tokens(2).toDouble, tokens(3).toDouble))
}).toList // Convert from an Iterator to List

回答by Atiq

There are ways to convert a Tuple to List or Seq, One way is

有多种方法可以将元组转换为列表或序列,一种方法是

scala> (1,2,3).productIterator.toList
res12: List[Any] = List(1, 2, 3)

But as you can see that the return type is Anyand NOT an INTEGER

但正如你所看到的,返回类型是Any而不是INTEGER

For converting into different types you use Hlist of https://github.com/milessabin/shapeless

要转换为不同类型,请使用https://github.com/milessabin/shapeless 的Hlist