Spark部分:Scala中 zip或者zipWithIndex的用法

使用zipWithIndex或者zip方法来自动地创建一个计数器,假设你有一个有序集合days,那么你可以使用zipWithIndex和counter来打印带有计数器的集合元素:

scala> val days = Array("Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday")
days: Array[String] = Array(Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)
 
scala> days.zipWithIndex.foreach{case(day,count) => println(s"$count is $day")}
0 is Sunday
1 is Monday
2 is Tuesday
3 is Wednesday
4 is Thursday
5 is Friday
6 is Saturday

同样,可以使用for循环来打印计数器和集合元素

scala> for((day,count) <- days.zipWithIndex) {
     |   println(s"$count is $day")
     | }
0 is Sunday
1 is Monday
2 is Tuesday
3 is Wednesday
4 is Thursday
5 is Friday
6 is Saturday

zipWithIndex的计数器都是从0开始,如果你想指定开始的值,那么你可以使用zip Stream:

scala> for((day,count) <- days.zip(Stream from 1)) {
     |   println(s"$count is $day")
     | }
1 is Sunday
2 is Monday
3 is Tuesday
4 is Wednesday
5 is Thursday
6 is Friday
7 is Saturday

zip和zipWithIndex方法都返回一个有序二元祖集合。因此,你的foreach方法也可以写成下面这样,虽然这比起解决方案中的方法,可读性略差。

scala> days.zipWithIndex.foreach(d => println(s"${d._2} is ${d._1}"))
0 is Sunday
1 is Monday
2 is Tuesday
3 is Wednesday
4 is Thursday
5 is Friday
6 is Saturday

 在之前的例子中我们曾经见过,可以通过一个for循环加range来创建这个计数器:

scala> val fruits = Array("apple", "banana", "orange")
fruits: Array[String] = Array(apple, banana, orange)
 
scala> for (i <- 0 until fruits.size) println(s"element $i is ${fruits(i)}")
element 0 is apple
element 1 is banana
element 2 is orange

Spark部分:Scala中 zip或者zipWithIndex的用法_第1张图片

你可能感兴趣的:(机器学习)