[Scala]元組Zip
元組 這個詞是從中國書及翻譯的詞。
元組:不同類型的聚集。(引用 快學Scala書籍)
Zip基本語法:
- zipWithInedx
1: scala> val employeeWithInedx=List("Andy", "Benson", "Mark").zipWithIndex2: employeeWithInedx: List[(String, Int)] = List((Andy,0), (Benson,1), (Mark,2))
- zip(Stream from N)
1: scala> val employeeWithInedx=List("Andy", "Benson", "Mark").zip(Stream from 2)2: employeeWithInedx: List[(String, Int)] = List((Andy,2), (Benson,3), (Mark,4))
Scala:
- zip 是將兩個集合做合併,若其中一個集合數量不匹配時,則無法匹配的項目將被省略。
1: val symbols=Array("*","-","O")2: val counts=Array(1,2,3)
3: var pairs=symbols.zip(counts)
4: println(pairs)
Result:
By-藍小伙