Scala zip 拉链操作

import scala.runtime._  
  
val year = List(2010, 2010, 2010, 2016, 2016)  
year: List[Int] = List(2010, 2010, 2010, 2016, 2016)  
  
val month = List(1, 2, 3, 9, 10)  
month: List[Int] = List(1, 2, 3, 9, 10)  
   
val price = List(13.8, 32, 62.9, 66, 88, 99)  
price: List[Double] = List(13.8, 32.0, 62.9, 66.0, 88.0, 99.0)  
 
val profit = List(1.1, 2.2, 3.3, 4.4, 5.5)  
profit: List[Double] = List(1.1, 2.2, 3.3, 4.4, 5.5)  
 
val res1 = year zip month  
res1: List[(Int, Int)] = List((2010,1), (2010,2), (2010,3), (2016,9), (2016,10))  
  
res1.foreach(println)  
(2010,1)  
(2010,2)  
(2010,3)  
(2016,9)  
(2016,10)  
 
  
val res2 = price zip profit  
res2: List[(Double, Double)] = List((13.8,1.1), (32.0,2.2), (62.9,3.3), (66.0,4.4), (88.0,5.5))  
  
res2.foreach(println)  
(13.8,1.1)  
(32.0,2.2)  
(62.9,3.3)  
(66.0,4.4)  
(88.0,5.5)  

  
val res3 = res1 zip res2  
res3: List[((Int, Int), (Double, Double))] = List(((2010,1),(13.8,1.1)), ((2010,2),(32.0,2.2)), ((2010,3),(62.9,3.3)), ((2016,9),(66.0,4.4)), ((2016,10),(88.0,5.5)))  
  
res3.foreach(println)  
((2010,1),(13.8,1.1))  
((2010,2),(32.0,2.2))  
((2010,3),(62.9,3.3))  
((2016,9),(66.0,4.4))  
((2016,10),(88.0,5.5))  

  
val spark1 = List("Vector", "Feature")  
spark1: List[String] = List(Vector, Feature)  

val spark2 = List("Scala", "SQL", "MLlib", "GraphX", "Streaming")  
spark2: List[String] = List(Scala, SQL, MLlib, GraphX, Streaming)  

val res4 = spark1 zip spark2  
res4: List[(String, String)] = List((Vector,Scala), (Feature,SQL))  
  
res4.foreach(println)  
(Vector,Scala)  
(Feature,SQL)  

// a.zipAll(b, thisElem, thatElem),a短,用thisElem填补;b短,用thatElem填补  
  
val res5 = spark1.zipAll(spark2, "DataFrame", "Pipeline")  
res5: List[(String, String)] = List((Vector,Scala), (Feature,SQL), (DataFrame,MLlib), (DataFrame,GraphX), (DataFrame,Streaming))  
  
res5.foreach(println)  
(Vector,Scala)  
(Feature,SQL)  
(DataFrame,MLlib)  
(DataFrame,GraphX)  
(DataFrame,Streaming)  

  
val res6 = spark2.zipAll(spark1, "DataFrame", "Pipeline")  
res6: List[(String, String)] = List((Scala,Vector), (SQL,Feature), (MLlib,Pipeline), (GraphX,Pipeline), (Streaming,Pipeline))  
  
res6.foreach(println)  
(Scala,Vector)  
(SQL,Feature)  
(MLlib,Pipeline)  
(GraphX,Pipeline)  
(Streaming,Pipeline)  
  
res6.par.foreach(println)  
(Scala,Vector)  
(SQL,Feature)  
(MLlib,Pipeline)  
(GraphX,Pipeline)  
(Streaming,Pipeline)  

  
val res7 = res6.zipWithIndex  
res7: List[((String, String), Int)] = List(((Scala,Vector),0), ((SQL,Feature),1), ((MLlib,Pipeline),2), ((GraphX,Pipeline),3), ((Streaming,Pipeline),4))  
  
res7.foreach(println)  
((Scala,Vector),0)  
((SQL,Feature),1)  
((MLlib,Pipeline),2)  
((GraphX,Pipeline),3)  
((Streaming,Pipeline),4)  

  
val a = Array(  
     | (1, 10),  
     | (2, 11),  
     | (3, 12),  
     | (4, 13),  
     | (5, 14))  
a: Array[(Int, Int)] = Array((1,10), (2,11), (3,12), (4,13), (5,14))  
 
val b = a.unzip  
b: (Array[Int], Array[Int]) = (Array(1, 2, 3, 4, 5),Array(10, 11, 12, 13, 14))  
 
val c = List(  
     | (1, 10, "a"),  
     | (2, 11, "b"),  
     | (3, 12, "c"),  
     | (4, 13, "d"),  
     | (5, 14, "e"))  
c: List[(Int, Int, String)] = List((1,10,a), (2,11,b), (3,12,c), (4,13,d), (5,14,e))  
 
val d = c.unzip3  
d: (List[Int], List[Int], List[String]) = (List(1, 2, 3, 4, 5),List(10, 11, 12, 13, 14),List(a, b, c, d, e))  
 
d._3.par.foreach(println)  
a  
c  
b  
d  

  

posted @ 2014-12-07 16:17  智能先行者  阅读(1691)  评论(0)    收藏  举报