[英]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. 我需要将Seq [Array [String]]类型的数据拆分为两个Seq [Double]类型的项。
Sample data : ([4.0|1492168815],[11.0|1491916394],[2.0|1491812028]). 样本数据:([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. 我使用了
var action1, timestamp1 = seq.map(t => (t.split("|"))).flatten.asInstanceOf[Seq[Double]]
但没有得到预期的结果。 Looking out for valuable suggestions. 寻找有价值的建议。
Assuming your input is in format "[double1|double2]"
, 假设您输入的格式为
"[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 并将它们分为两个
Seq[Double]
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, 如果输入中可能包含非双精度数据,则应使用
Try
进行更安全的Double
转换,
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; 我们将
pat
定义的正则表达式应用于列表的每个项目,对每个项目的每个组进行元组化,最后将它们unzip
,以便将元组一分为二。 viz. 即
(List(4.0, 11.0, 2.0),List(1492168815, 1491916394, 1491812028))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.