I am wondering without creating a function, how can I filter out numbers from a list with both numbers and strings:
val a = sc.parallelize(List(“cat”,“horse”,4.0,3.5,2,“dog”))
I believe my question indeed is looking for how to use regex in Scala to find out matched pattern
----Updated on 20180302 11pm EST: Thanks to @Nyavro which is the closest answer, I slightly modified as below:
val doubles = a.collect {
case v: Double => v
case v: Int => v
}
Now I get:
res10: Array[Double] = Array(4.0, 3.5, 2.0)
Just be curious, can types be mixed in a collect result in Scala?
Thanks to all replies.
Use collect:
val doubles = a.collect {
case v: Double => v
}
To filter for elements of type Int
and Double
, and to retain their respective types, you might try this.
a.flatMap {
case v: Int => Some(v)
case v: Double => Some(v)
case _ => None
}
//res0: List[AnyVal] = List(4.0, 3.5, 2)
To help understand why this is a really bad idea, read this question , and its answers.
You can use isInstanceOf
to check whether an element of your list is a string.
val l = List("cat","horse",4.0,3.5,2,"dog")
l.filter(_.isInstanceOf[String])
>> List[Any] = List(cat, horse, dog)
Regex is (largely) irrelevant here because you do not have strings, you have a List[Any]
that you're turning into an RDD[Any]
. (The RDD is largely irrelevant here, too, except RDDs have no filterNot
and Lists do--I can't tell if you want to keep the strings or drop the strings.)
Note also that filter
takes a function as an argument--having some function here is inescapable, even if it's anonymous, as it is in my example.
I have an inkling, though, that I've given an answer to the opposite of what you're asking, and you have an RDD[String]
that you want to convert to RDD[Double]
, throwing away the strings that don't convert. In that case, I would try to convert the strings to doubles, wrapping that in a Try
and check for success, using the result to filter:
def isDouble(s: String) = Try(s.toDouble).isSuccess
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.