简体   繁体   English

Scala将字符串拆分为元组

[英]Scala split string to tuple

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. 我试图转换成List [(String,String,Point)],这样前两个分裂是列表中的前两个元素,最后两个是Point。 I am doing the following but it doesn't seem to work: 我正在做以下但它似乎不起作用:

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

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))

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)))
    })

You are not converting the third and fourth tokens into a Point , nor are you converting the lines into a List . 您没有将第三个和第四个标记转换为Point ,也没有将行转换为List Also, you are not rendering each element as a Tuple3 , but as a List . 此外,您不是将每个元素渲染为Tuple3 ,而是渲染为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
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]. 对于好奇的,@ in模式匹配将变量绑定到模式,所以points @ _*将变量点绑定到模式* _和* _匹配数组的其余部分,所以点最终成为Seq [串]。

There are ways to convert a Tuple to List or Seq, One way is 有一些方法可以将元组转换为List或Seq,其中一种方法是

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

But as you can see that the return type is Any and 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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM