简体   繁体   中英

Scala: How to convert a Seq[Array[String]] into Seq[Double]?

I need to split up the data in Seq[Array[String]] type into two Seq[Double] type items.

Sample data : ([4.0|1492168815],[11.0|1491916394],[2.0|1491812028]).

I used var action1, timestamp1 = seq.map(t => (t.split("|"))).flatten.asInstanceOf[Seq[Double]] but didn't get the results as expected. Looking out for valuable suggestions.

Assuming your input is in format "[double1|double2]" ,

scala> Seq("[4.0|1492168815]","[11.0|1491916394]","[2.0|1491812028]")
res72: Seq[String] = List([4.0|1492168815], [11.0|1491916394], [2.0|1491812028])

drop [ and ] , then split by \\\\| , | is a metacharacter in regex.

scala> res72.flatMap {_.dropRight(1).drop(1).split("\\|").toList}.map{_.toDouble}
res74: Seq[Double] = List(4.0, 1.492168815E9, 11.0, 1.491916394E9, 2.0, 1.491812028E9)

Or you can do

scala> val actTime = seq.flatMap(t => t.map(x => { val temp = x.split("\\|"); (temp(0), temp(1))}))
actTime: Seq[(String, String)] = List((4.0,1492168815), (11.0,1491916394), (2.0,1491812028))

And to separate them into two Seq[Double] you can do

scala> val action1 = actTime.map(_._1.toDouble)
action1: Seq[Double] = List(4.0, 11.0, 2.0)

scala> val timestamp1 = actTime.map(_._2.toDouble)
timestamp1: Seq[Double] = List(1.492168815E9, 1.491916394E9, 1.491812028E9)

If there could be non-double data in input, you should use Try for safer Double conversion,

scala> Seq("[4.0|1492168815]","[11.0|1491916394]","[2.0|1491812028]", "[abc|abc]")
res75: Seq[String] = List([4.0|1492168815], [11.0|1491916394], [2.0|1491812028], [abc|abc])

scala> import scala.util.Success
import scala.util.Success

scala> import scala.util.Try
import scala.util.Try

scala> res75.flatMap {_.dropRight(1).drop(1).split("\\|").toList}
            .map{d => Try(d.toDouble)}
            .collect {case Success(x) => x }
res83: Seq[Double] = List(4.0, 1.492168815E9, 11.0, 1.491916394E9, 2.0, 1.491812028E9) 

Extract each item in the input list with regular expression groups delimited with [ , | and ] ,

val pat = "\\[(.*)\\|(.*)\\]".r

Hence if we suppose an input such as

val xs = List("[4.0|1492168815]","[11.0|1491916394]","[2.0|1491812028]")

consider

xs.map { v => val pat(a,b) = v; (a.toDouble, b.toLong) }.unzip

where we apply the regex defined in pat onto each item of the list, tuple each group for each item and finally unzip them so that we bisect the tuples into separate collections; viz.

(List(4.0, 11.0, 2.0),List(1492168815, 1491916394, 1491812028))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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