如何检查列表中的所有元素是否都是数字?
How to check whether all elements in list are numeric?
我有一个这种形式的数据,这基本上是一个巨大的数据。
val data: Array[(Int, Iterable[String])] =
Array(
(34,List("sdkfj",7, 2, 5, 3, 1, 9, 2, 1, 4)),
(4,List(7, 14, 4, 5, 11, 9, 5, 4, 1))
)
我想在可迭代项上应用一个函数 say isNumeric
,它应该 return 我按以下方式输出
Array((34,false), (4,true))
基本上,如果列表中的任何元素不是数字,我想要一个 false
,否则 true
。
我试过这个功能
def isNumeric(input: String): Boolean = input.forall(_.isDigit)
但在这种情况下,我得到了一个布尔值列表,而我想要整个列表的单个布尔值结果。
您需要在内部列表上调用 .forall
:
scala> def isNumeric(input: String): Boolean = input.forall(_.isDigit)
isNumeric: (input: String)Boolean
scala> val a:Array[(Int,List[String])] = Array((34,List("sdkfj","7", "2", "5", "3", "1", "9", "2", "1", "4")), (4,List("7", "14", "4", "5", "11", "9", "5", "4", "1")))
a: Array[(Int, List[String])] = Array((34,List(sdkfj, 7, 2, 5, 3, 1, 9, 2, 1, 4)),
(4, List(7, 14, 4, 5, 11, 9, 5, 4, 1)))
scala> a.map { case (k, l) => (k, l.forall(isNumeric)) }
res0: Array[(Int, Boolean)] = Array((34,false), (4,true))
编辑
如果要跳过某些元素的验证,可以在使用 .forall
之前使用 .filter
(运行 仅在子列表上进行验证):
scala> val a:Array[(Int,List[String])] = Array((34,List("NA","7", "3")))
a: Array[(Int, List[String])] = Array((34,List(NA, 7, 3)))
// Not sure what an NA is, so...
scala> def isNA(s:String) = s == "NA"
isNA: (s: String)Boolean
// Using .filterNot here since you want to exclude all `NA`s
// You can also use .filter(!isNA(_))
// or .withFilter(!isNA(_)) which should be faster/more efficient
scala> a.map{ case (k, l) => (k, l.filterNot(isNA).forall(isNumeric)) }
res0: Array[(Int, Boolean)] = Array((34,true))
请注意,您还可以将 .isNumeric
方法更改为 .isNumericOrNA
(从而跳过对 filter
的需要),尽管这可能有点令人困惑,具体取决于 NA
是.
注意:请参阅 Scala API 以了解 .filter
和 .withFilter
之间差异的解释。
我有一个这种形式的数据,这基本上是一个巨大的数据。
val data: Array[(Int, Iterable[String])] =
Array(
(34,List("sdkfj",7, 2, 5, 3, 1, 9, 2, 1, 4)),
(4,List(7, 14, 4, 5, 11, 9, 5, 4, 1))
)
我想在可迭代项上应用一个函数 say isNumeric
,它应该 return 我按以下方式输出
Array((34,false), (4,true))
基本上,如果列表中的任何元素不是数字,我想要一个 false
,否则 true
。
我试过这个功能
def isNumeric(input: String): Boolean = input.forall(_.isDigit)
但在这种情况下,我得到了一个布尔值列表,而我想要整个列表的单个布尔值结果。
您需要在内部列表上调用 .forall
:
scala> def isNumeric(input: String): Boolean = input.forall(_.isDigit)
isNumeric: (input: String)Boolean
scala> val a:Array[(Int,List[String])] = Array((34,List("sdkfj","7", "2", "5", "3", "1", "9", "2", "1", "4")), (4,List("7", "14", "4", "5", "11", "9", "5", "4", "1")))
a: Array[(Int, List[String])] = Array((34,List(sdkfj, 7, 2, 5, 3, 1, 9, 2, 1, 4)),
(4, List(7, 14, 4, 5, 11, 9, 5, 4, 1)))
scala> a.map { case (k, l) => (k, l.forall(isNumeric)) }
res0: Array[(Int, Boolean)] = Array((34,false), (4,true))
编辑
如果要跳过某些元素的验证,可以在使用 .forall
之前使用 .filter
(运行 仅在子列表上进行验证):
scala> val a:Array[(Int,List[String])] = Array((34,List("NA","7", "3")))
a: Array[(Int, List[String])] = Array((34,List(NA, 7, 3)))
// Not sure what an NA is, so...
scala> def isNA(s:String) = s == "NA"
isNA: (s: String)Boolean
// Using .filterNot here since you want to exclude all `NA`s
// You can also use .filter(!isNA(_))
// or .withFilter(!isNA(_)) which should be faster/more efficient
scala> a.map{ case (k, l) => (k, l.filterNot(isNA).forall(isNumeric)) }
res0: Array[(Int, Boolean)] = Array((34,true))
请注意,您还可以将 .isNumeric
方法更改为 .isNumericOrNA
(从而跳过对 filter
的需要),尽管这可能有点令人困惑,具体取决于 NA
是.
注意:请参阅 Scala API 以了解 .filter
和 .withFilter
之间差异的解释。