Scala - 使用.indexOf()和.indexWhere()

时间:2017-04-06 16:33:31

标签: scala apache-spark indexof

我有一个像下面这样的元组:

(Age, List(19,17,11,3,2))

我希望得到第一个元素的位置,它们在列表中的位置大于它们的值。要做到这一点,我尝试使用.indexOf()和.indexWhere(),但我可能找不到完全正确的语法,所以我一直得到:

  

value indexWhere不是org.apache.spark.rdd.RDD的成员[(String,   可迭代[INT])]

到目前为止我的代码是:

val test =("Age", List(19,17,11,3,2))
test.indexWhere(_.2(_)<=_.2(_).indexOf(_.2(_)) )

我还在这里搜索了没有结果的文档:http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.List

1 个答案:

答案 0 :(得分:3)

If you want to perform this for each element in an RDD, you can use RDD's mapValues (which would only map the right-hand-side of the tuple) and pass a function that uses indexWhere:

rdd.mapValues(_.zipWithIndex.indexWhere { case (v, i) => i+1 > v} + 1)

Notes:

  • Your example seems wrong, if you want the last matching item it should be 5 (position of 2) and not 4
  • You did not define what should be done when no item matches your condition, e.g. for List(0,0,0) - in this case the result would be 0 but not sure that's what you need