scala元組及拉鍊操作:
scala裡面的元組是可是不同資料型別的
scala>("scala",1)
res45: (string, int) = (scala,1)
scala>val t = ("scala",100l,3.14,("spark",1))
t:(string, long, double, (string, int))= (scala,100,3.14,(spark,1))
scala>t._1//元組內空的提取,以下標為1開始,並用._後跟數字的方式進行提取內容
res46: string = scala
scala>t._3
res47: double = 3.14
scala>t._4._1
res48: string = spark
scala>val t,(a,b,c,d) = ("scala",100l,3.14,("spark",1))//可以定義元組時給予變數名,如 t,(a, b, c, d),使用時直接用變數名a,b,c,d即可訪問對應內容
t: (string, long, double, (string, int)) = (scala,100,3.14,(spark,1))
a: string = scala
b: long = 100
c: double = 3.14
d: (string, int) = (spark,1)
scala>a
res49: string = scala
scala>val arr = array(("tingting",1),("ningning",3),("huihui",5))
arr: array[(string, int)] = array((tingting,1), (ningning,3), (huihui,5))
scala>val mp =arr.tomap
mp:scala.collection.immutable.map[string,int] = map(tingting -> 1, ningning -> 3, huihui -> 5)
scala拉鍊操作:
scala>val arr1 = array("dog","cat","mouse")
arr1: array[string] = array(dog, cat, mouse)
scala>val arr2 = array(1,3,5)
arr2: array[int] = array(1, 3, 5)
scala>arr1 zip arr2//拉鍊操作符
res50: array[(string, int)] = array((dog,1), (cat,3), (mouse,5))
scala>arr2 zip arr1//拉鍊操作符
res51: array[(int, string)] = array((1,dog), (3,cat), (5,mouse))
scala>arr1.zip(arr2)//拉鍊操作符
res52: array[(string, int)] = array((dog,1), (cat,3), (mouse,5))
scala>val arr3 = array(1,2,3,4,5)//生成乙個比arr1元素多的陣列,用少的去zip多的
arr3: array[int] = array(1, 2, 3, 4, 5)
scala>arr1 zip arr3//會把長的給擷取掉了
res53: array[(string, int)] =array((dog,1), (cat,2), (mouse,3))
列表及元組操作
03 建立列表 4種方法 增 5 刪 3 統計元素出現次數,返回列表長度,建立a a 1,2,3,4,5 print a b ab,cc,dd,ce c list b print c d range 10,20,2 print type d print list d 增加元素 a.insert 3,...
Scala對映和元組
import scala.collection.mutable object helloworld for k,v 對映 處理k和v 如果你需要訪問鍵或值,可以使用keyset和values方法,values返回乙個iterable 要反轉乙個對映 即交換鍵和值的位置 可以用 for k,v 對映 ...
Scala 對映和元組
val scores map jim 10,tom 20 sam 44 key value,key,value 兩種方式表示,不可變對映,val s scala.collection.mutable.map jim 10,tom 20 sam 44 可變對映 val s1 new scala.col...