Scala是数据挖掘算法领域最有力的编程语言之一,语言本身是面向函数,这也符合了数据挖掘算法的常用场景:在原始数据集上应用一系列的变换,语言本身也对集合操作提供了众多强大的函数,本文将以List类型为例子,介绍常见的集合变换操作。
一、常用操作符(操作符其实也是函数)
++ ++[B](that: GenTraversableOnce[B]): List[B] 从列表的尾部添加另外一个列表
++: ++:[B >: A, That](that: collection.Traversable[B])(implicit bf: CanBuildFrom[List[A], B, That]): That 在列表的头部添加一个列表
+: +:(elem: A): List[A] 在列表的头部添加一个元素
:+ :+(elem: A): List[A] 在列表的尾部添加一个元素
:: ::(x: A): List[A] 在列表的头部添加一个元素
::: :::(prefix: List[A]): List[A] 在列表的头部添加另外一个列表
:\ :[B](z: B)(op: (A, B) ⇒ B): B 与foldRight等价
<code class="hljs livecodeserver has-numbering">val left = List(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>) val <span class="hljs-constant">right</span> = List(<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>) <span class="hljs-comment"> //以下操作等价</span> left ++ <span class="hljs-constant">right</span> <span class="hljs-comment"> // List(1,2,3,4,5,6)</span> left ++: <span class="hljs-constant">right</span> <span class="hljs-comment"> // List(1,2,3,4,5,6)</span> <span class="hljs-constant">right</span>.++:(left) <span class="hljs-comment"> // List(1,2,3,4,5,6)</span> <span class="hljs-constant">right</span>.:::(left) <span class="hljs-comment"> // List(1,2,3,4,5,6)</span> <span class="hljs-comment"> //以下操作等价</span> <span class="hljs-number">0</span> +: left <span class="hljs-comment"> //List(0,1,2,3)</span> left.+:(<span class="hljs-number">0</span>) <span class="hljs-comment"> //List(0,1,2,3)</span> <span class="hljs-comment"> //以下操作等价</span> left :+ <span class="hljs-number">4</span> <span class="hljs-comment"> //List(1,2,3,4)</span> left.:+(<span class="hljs-number">4</span>) <span class="hljs-comment"> //List(1,2,3,4)</span> <span class="hljs-comment"> //以下操作等价</span> <span class="hljs-number">0</span> :: left <span class="hljs-comment"> //List(0,1,2,3)</span> left.::(<span class="hljs-number">0</span>) <span class="hljs-comment"> //List(0,1,2,3)</span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li></ul>
看到这里大家应该跟我一样有一点晕吧,怎么这么多奇怪的操作符,这里给大家一个提示,任何以冒号结果的操作符,都是右绑定的,即 0 :: List(1,2,3) = List(1,2,3).::(0) = List(0,1,2,3) 从这里可以看出操作::其实是右边List的操作符,而非左边Int类型的操作符
二、常用变换操作
1.map
map[B](f: (A) ⇒ B): List[B]
定义一个变换,把该变换应用到列表的每个元素中,原列表不变,返回一个新的列表数据
Example1 平方变换
<code class="hljs fsharp has-numbering"><span class="hljs-keyword">val</span> nums = List(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>) <span class="hljs-keyword">val</span> square = (x: Int) => x*x <span class="hljs-keyword">val</span> squareNums1 = nums.map(num => num*num) <span class="hljs-comment">//List(1,4,9)</span> <span class="hljs-keyword">val</span> squareNums2 = nums.map(math.pow(_,<span class="hljs-number">2</span>)) <span class="hljs-comment">//List(1,4,9)</span> <span class="hljs-keyword">val</span> squareNums3 = nums.map(square) <span class="hljs-comment">//List(1,4,9)</span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>
Example2 保存文本数据中的某几列
<code class="hljs livecodeserver has-numbering">val <span class="hljs-keyword">text</span> = List(<span class="hljs-string">"Homeway,25,Male"</span>,<span class="hljs-string">"XSDYM,23,Female"</span>) val usersList = <span class="hljs-keyword">text</span>.map(_.<span class="hljs-built_in">split</span>(<span class="hljs-string">","</span>)(<span class="hljs-number">0</span>)) val usersWithAgeList = <span class="hljs-keyword">text</span>.map(<span class="hljs-built_in">line</span> => { val fields = <span class="hljs-built_in">line</span>.<span class="hljs-built_in">split</span>(<span class="hljs-string">","</span>) val user = fields(<span class="hljs-number">0</span>) val age = fields(<span class="hljs-number">1</span>).toInt (user,age) }) </code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>
2.flatMap, flatten
flatten: flatten[B]: List[B] 对列表的列表进行平坦化操作 flatMap: flatMap[B](f: (A) ⇒ GenTraversableOnce[B]): List[B] map之后对结果进行flatten
定义一个变换f, 把f应用列表的每个元素中,每个f返回一个列表,最终把所有列表连结起来。
<code class="hljs avrasm has-numbering">val text = List(<span class="hljs-string">"A,B,C"</span>,<span class="hljs-string">"D,E,F"</span>) val textMapped = text<span class="hljs-preprocessor">.map</span>(_<span class="hljs-preprocessor">.split</span>(<span class="hljs-string">","</span>)<span class="hljs-preprocessor">.toList</span>) // List(List(<span class="hljs-string">"A"</span>,<span class="hljs-string">"B"</span>,<span class="hljs-string">"C"</span>),List(<span class="hljs-string">"D"</span>,<span class="hljs-string">"E"</span>,<span class="hljs-string">"F"</span>)) val textFlattened = textMapped<span class="hljs-preprocessor">.flatten</span> // List(<span class="hljs-string">"A"</span>,<span class="hljs-string">"B"</span>,<span class="hljs-string">"C"</span>,<span class="hljs-string">"D"</span>,<span class="hljs-string">"E"</span>,<span class="hljs-string">"F"</span>) val textFlatMapped = text<span class="hljs-preprocessor">.flatMap</span>(_<span class="hljs-preprocessor">.split</span>(<span class="hljs-string">","</span>)<span class="hljs-preprocessor">.toList</span>) // List(<span class="hljs-string">"A"</span>,<span class="hljs-string">"B"</span>,<span class="hljs-string">"C"</span>,<span class="hljs-string">"D"</span>,<span class="hljs-string">"E"</span>,<span class="hljs-string">"F"</span>) </code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>
3.reduce
reduce[A1 >: A](op: (A1, A1) ⇒ A1): A1
定义一个变换f, f把两个列表的元素合成一个,遍历列表,最终把列表合并成单一元素
Example 列表求和
<code class="hljs fsharp has-numbering"> <span class="hljs-keyword">val</span> nums = List(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>) <span class="hljs-keyword">val</span> sum1 = nums.reduce((a,b) => a+b) <span class="hljs-comment">//6</span> <span class="hljs-keyword">val</span> sum2 = nums.reduce(_+_) <span class="hljs-comment">//6</span> <span class="hljs-keyword">val</span> sum3 = nums.sum <span class="hljs-comment">//6</span> </code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li></ul>
4.reduceLeft,reduceRight
reduceLeft: reduceLeft[B >: A](f: (B, A) ⇒ B): B
reduceRight: reduceRight[B >: A](op: (A, B) ⇒ B): B
reduceLeft从列表的左边往右边应用reduce函数,reduceRight从列表的右边往左边应用reduce函数
Example
<code class="hljs fsharp has-numbering"> <span class="hljs-keyword">val</span> nums = List(<span class="hljs-number">2.0</span>,<span class="hljs-number">2.0</span>,<span class="hljs-number">3.0</span>) <span class="hljs-keyword">val</span> resultLeftReduce = nums.reduceLeft(math.pow) <span class="hljs-comment">// = pow( pow(2.0,2.0) , 3.0) = 64.0</span> <span class="hljs-keyword">val</span> resultRightReduce = nums.reduceRight(math.pow) <span class="hljs-comment">// = pow(2.0, pow(2.0,3.0)) = 256.0</span> </code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>
5.fold,foldLeft,foldRight
fold: fold[A1 >: A](z: A1)(op: (A1, A1) ⇒ A1): A1 带有初始值的reduce,从一个初始值开始,从左向右将两个元素合并成一个,最终把列表合并成单一元素。
foldLeft: foldLeft[B](z: B)(f: (B, A) ⇒ B): B 带有初始值的reduceLeft
foldRight: foldRight[B](z: B)(op: (A, B) ⇒ B): B 带有初始值的reduceRight
<code class="hljs fsharp has-numbering"> <span class="hljs-keyword">val</span> nums = List(<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>) <span class="hljs-keyword">val</span> sum = nums.fold(<span class="hljs-number">1</span>)(_+_) <span class="hljs-comment">// = 1+2+3+4 = 9</span> <span class="hljs-keyword">val</span> nums = List(<span class="hljs-number">2.0</span>,<span class="hljs-number">3.0</span>) <span class="hljs-keyword">val</span> result1 = nums.foldLeft(<span class="hljs-number">4.0</span>)(math.pow) <span class="hljs-comment">// = pow(pow(4.0,2.0),3.0) = 4096</span> <span class="hljs-keyword">val</span> result2 = nums.foldRight(<span class="hljs-number">1.0</span>)(math.pow) <span class="hljs-comment">// = pow(1.0,pow(2.0,3.0)) = 8.0</span> </code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li></ul>
6.sortBy,sortWith,sorted
sortBy: sortBy[B](f: (A) ⇒ B)(implicit ord: math.Ordering[B]): List[A] 按照应用函数f之后产生的元素进行排序
sorted: sorted[B >: A](implicit ord: math.Ordering[B]): List[A] 按照元素自身进行排序
sortWith: sortWith(lt: (A, A) ⇒ Boolean): List[A] 使用自定义的比较函数进行排序
<code class="hljs coffeescript has-numbering">val nums = List(<span class="hljs-number">1</span>,<span class="hljs-number">3</span>,<span class="hljs-number">2</span>,<span class="hljs-number">4</span>) val sorted = nums.sorted <span class="hljs-regexp">//</span>List(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>) val users = List((<span class="hljs-string">"HomeWay"</span>,<span class="hljs-number">25</span>),(<span class="hljs-string">"XSDYM"</span>,<span class="hljs-number">23</span>)) val sortedByAge = users.sortBy{<span class="hljs-reserved">case</span><span class="hljs-function"><span class="hljs-params">(user,age)</span> =></span> age} <span class="hljs-regexp">//</span>List((<span class="hljs-string">"XSDYM"</span>,<span class="hljs-number">23</span>),(<span class="hljs-string">"HomeWay"</span>,<span class="hljs-number">25</span>)) val sortedWith = users.sortWith{<span class="hljs-reserved">case</span><span class="hljs-function"><span class="hljs-params">(user1,user2)</span> =></span> user1._2 < user2._2} <span class="hljs-regexp">//</span>List((<span class="hljs-string">"XSDYM"</span>,<span class="hljs-number">23</span>),(<span class="hljs-string">"HomeWay"</span>,<span class="hljs-number">25</span>)) </code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li></ul>
7.filter, filterNot
filter: filter(p: (A) ⇒ Boolean): List[A]
filterNot: filterNot(p: (A) ⇒ Boolean): List[A]
filter 保留列表中符合条件p的列表元素 , filterNot,保留列表中不符合条件p的列表元素
<code class="hljs fsharp has-numbering"><span class="hljs-keyword">val</span> nums = List(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>) <span class="hljs-keyword">val</span> odd = nums.filter( _ % <span class="hljs-number">2</span> != <span class="hljs-number">0</span>) <span class="hljs-comment">// List(1,3)</span> <span class="hljs-keyword">val</span> even = nums.filterNot( _ % <span class="hljs-number">2</span> != <span class="hljs-number">0</span>) <span class="hljs-comment">// List(2,4)</span> </code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li></ul>
8.count
count(p: (A) ⇒ Boolean): Int
计算列表中所有满足条件p的元素的个数,等价于 filter(p).length
val nums = List(-1,-2,0,1,2) val plusCnt1 = nums.count( > 0) val plusCnt2 = nums.filter( > 0).length
9. diff, union, intersect
diff:diff(that: collection.Seq[A]): List[A] 保存列表中那些不在另外一个列表中的元素,即从集合中减去与另外一个集合的交集
union : union(that: collection.Seq[A]): List[A] 与另外一个列表进行连结
intersect: intersect(that: collection.Seq[A]): List[A] 与另外一个集合的交集
<code class="hljs fsharp has-numbering"><span class="hljs-keyword">val</span> nums1 = List(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>) <span class="hljs-keyword">val</span> nums2 = List(<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>) <span class="hljs-keyword">val</span> diff1 = nums1 diff nums2 <span class="hljs-comment">// List(1)</span> <span class="hljs-keyword">val</span> diff2 = nums2.diff(num1) <span class="hljs-comment">// List(4)</span> <span class="hljs-keyword">val</span> union1 = nums1 union nums2 <span class="hljs-comment">// List(1,2,3,2,3,4)</span> <span class="hljs-keyword">val</span> union2 = nums2 ++ nums1 <span class="hljs-comment">// List(2,3,4,1,2,3)</span> <span class="hljs-keyword">val</span> intersection = nums1 intersect nums2 <span class="hljs-comment">//List(2,3)</span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li></ul>
10.distinct
distinct: List[A] 保留列表中非重复的元素,相同的元素只会被保留一次
<code class="hljs mathematica has-numbering">val list = <span class="hljs-keyword">List</span>(<span class="hljs-string">"A"</span>,<span class="hljs-string">"B"</span>,<span class="hljs-string">"C"</span>,<span class="hljs-string">"A"</span>,<span class="hljs-string">"B"</span>) val distincted = list.distinct // <span class="hljs-keyword">List</span>(<span class="hljs-string">"A"</span>,<span class="hljs-string">"B"</span>,<span class="hljs-string">"C"</span>)</code><ul style="display: block;" class="pre-numbering"><li>1</li></ul>
11.groupBy, grouped
groupBy : groupBy[K](f: (A) ⇒ K): Map[K, List[A]] 将列表进行分组,分组的依据是应用f在元素上后产生的新元素
grouped: grouped(size: Int): Iterator[List[A]] 按列表按照固定的大小进行分组
<code class="hljs mathematica has-numbering">val data = <span class="hljs-keyword">List</span>((<span class="hljs-string">"HomeWay"</span>,<span class="hljs-string">"Male"</span>),(<span class="hljs-string">"XSDYM"</span>,<span class="hljs-string">"Femail"</span>),(<span class="hljs-string">"Mr.Wang"</span>,<span class="hljs-string">"Male"</span>)) val group1 = data.groupBy(_._2) // = <span class="hljs-keyword">Map</span>(<span class="hljs-string">"Male"</span> -> <span class="hljs-keyword">List</span>((<span class="hljs-string">"HomeWay"</span>,<span class="hljs-string">"Male"</span>),(<span class="hljs-string">"Mr.Wang"</span>,<span class="hljs-string">"Male"</span>)),<span class="hljs-string">"Female"</span> -> <span class="hljs-keyword">List</span>((<span class="hljs-string">"XSDYM"</span>,<span class="hljs-string">"Femail"</span>))) val group2 = data.groupBy<span class="hljs-list">{case (name,sex) => sex}</span> // = <span class="hljs-keyword">Map</span>(<span class="hljs-string">"Male"</span> -> <span class="hljs-keyword">List</span>((<span class="hljs-string">"HomeWay"</span>,<span class="hljs-string">"Male"</span>),(<span class="hljs-string">"Mr.Wang"</span>,<span class="hljs-string">"Male"</span>)),<span class="hljs-string">"Female"</span> -> <span class="hljs-keyword">List</span>((<span class="hljs-string">"XSDYM"</span>,<span class="hljs-string">"Femail"</span>))) val fixSizeGroup = data.grouped(<span class="hljs-number">2</span>).toList // = <span class="hljs-keyword">Map</span>(<span class="hljs-string">"Male"</span> -> <span class="hljs-keyword">List</span>((<span class="hljs-string">"HomeWay"</span>,<span class="hljs-string">"Male"</span>),(<span class="hljs-string">"XSDYM"</span>,<span class="hljs-string">"Femail"</span>)),<span class="hljs-string">"Female"</span> -> <span class="hljs-keyword">List</span>((<span class="hljs-string">"Mr.Wang"</span>,<span class="hljs-string">"Male"</span>))) </code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li></ul>
12.scan
scan[B >: A, That](z: B)(op: (B, B) ⇒ B)(implicit cbf: CanBuildFrom[List[A], B, That]): That
由一个初始值开始,从左向右,进行积累的op操作,这个比较难解释,具体的看例子吧。
<code class="hljs fsharp has-numbering"><span class="hljs-keyword">val</span> nums = List(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>) <span class="hljs-keyword">val</span> result = nums.scan(<span class="hljs-number">10</span>)(_+_) <span class="hljs-comment">// List(10,10+1,10+1+2,10+1+2+3) = List(10,11,12,13)</span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li></ul>
13.scanLeft,scanRight
scanLeft: scanLeft[B, That](z: B)(op: (B, A) ⇒ B)(implicit bf: CanBuildFrom[List[A], B, That]): That
scanRight: scanRight[B, That](z: B)(op: (A, B) ⇒ B)(implicit bf: CanBuildFrom[List[A], B, That]): That
scanLeft: 从左向右进行scan函数的操作,scanRight:从右向左进行scan函数的操作
<code class="hljs fsharp has-numbering"><span class="hljs-keyword">val</span> nums = List(<span class="hljs-number">1.0</span>,<span class="hljs-number">2.0</span>,<span class="hljs-number">3.0</span>) <span class="hljs-keyword">val</span> result = nums.scanLeft(<span class="hljs-number">2.0</span>)(math.pow) <span class="hljs-comment">// List(2.0,pow(2.0,1.0), pow(pow(2.0,1.0),2.0),pow(pow(pow(2.0,1.0),2.0),3.0) = List(2.0,2.0,4.0,64.0)</span> <span class="hljs-keyword">val</span> result = nums.scanRight(<span class="hljs-number">2.0</span>)(math.pow) <span class="hljs-comment">// List(2.0,pow(3.0,2.0), pow(2.0,pow(3.0,2.0)), pow(1.0,pow(2.0,pow(3.0,2.0))) = List(1.0,512.0,9.0,2.0)</span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li></ul>
14.take,takeRight,takeWhile
take : takeRight(n: Int): List[A] 提取列表的前n个元素 takeRight: takeRight(n: Int): List[A] 提取列表的最后n个元素 takeWhile: takeWhile(p: (A) ⇒ Boolean): List[A] 从左向右提取列表的元素,直到条件p不成立
<code class="hljs fsharp has-numbering"><span class="hljs-keyword">val</span> nums = List(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">4</span>,<span class="hljs-number">4</span>,<span class="hljs-number">4</span>,<span class="hljs-number">4</span>) <span class="hljs-keyword">val</span> left = nums.take(<span class="hljs-number">4</span>) <span class="hljs-comment">// List(1,1,1,1)</span> <span class="hljs-keyword">val</span> right = nums.takeRight(<span class="hljs-number">4</span>) <span class="hljs-comment">// List(4,4,4,4)</span> <span class="hljs-keyword">val</span> headNums = nums.takeWhile( _ == nums.head) <span class="hljs-comment">// List(1,1,1,1)</span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li></ul>
15.drop,dropRight,dropWhile
drop: drop(n: Int): List[A] 丢弃前n个元素,返回剩下的元素 dropRight: dropRight(n: Int): List[A] 丢弃最后n个元素,返回剩下的元素 dropWhile: dropWhile(p: (A) ⇒ Boolean): List[A] 从左向右丢弃元素,直到条件p不成立
<code class="hljs fsharp has-numbering"><span class="hljs-keyword">val</span> nums = List(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">4</span>,<span class="hljs-number">4</span>,<span class="hljs-number">4</span>,<span class="hljs-number">4</span>) <span class="hljs-keyword">val</span> left = nums.drop(<span class="hljs-number">4</span>) <span class="hljs-comment">// List(4,4,4,4)</span> <span class="hljs-keyword">val</span> right = nums.dropRight(<span class="hljs-number">4</span>) <span class="hljs-comment">// List(1,1,1,1)</span> <span class="hljs-keyword">val</span> tailNums = nums.dropWhile( _ == nums.head) <span class="hljs-comment">// List(4,4,4,4)</span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li></ul>
16.span, splitAt, partition
span : span(p: (A) ⇒ Boolean): (List[A], List[A]) 从左向右应用条件p进行判断,直到条件p不成立,此时将列表分为两个列表
splitAt: splitAt(n: Int): (List[A], List[A]) 将列表分为前n个,与,剩下的部分
partition: partition(p: (A) ⇒ Boolean): (List[A], List[A]) 将列表分为两部分,第一部分为满足条件p的元素,第二部分为不满足条件p的元素
<code class="hljs fsharp has-numbering"><span class="hljs-keyword">val</span> nums = List(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">2</span>,<span class="hljs-number">1</span>) <span class="hljs-keyword">val</span> (prefix,suffix) = nums.span( _ == <span class="hljs-number">1</span>) <span class="hljs-comment">// prefix = List(1,1,1), suffix = List(2,3,2,1)</span> <span class="hljs-keyword">val</span> (prefix,suffix) = nums.splitAt(<span class="hljs-number">3</span>) <span class="hljs-comment">// prefix = List(1,1,1), suffix = List(2,3,2,1)</span> <span class="hljs-keyword">val</span> (prefix,suffix) = nums.partition( _ == <span class="hljs-number">1</span>) <span class="hljs-comment">// prefix = List(1,1,1,1), suffix = List(2,3,2)</span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li></ul>
17.padTo
padTo(len: Int, elem: A): List[A]
将列表扩展到指定长度,长度不够的时候,使用elem进行填充,否则不做任何操作。
<code class="hljs fsharp has-numbering"> <span class="hljs-keyword">val</span> nums = List(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>) <span class="hljs-keyword">val</span> padded = nums.padTo(<span class="hljs-number">6</span>,<span class="hljs-number">2</span>) <span class="hljs-comment">// List(1,1,1,2,2,2)</span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li></ul>
18.combinations,permutations
combinations: combinations(n: Int): Iterator[List[A]] 取列表中的n个元素进行组合,返回不重复的组合列表,结果一个迭代器
permutations: permutations: Iterator[List[A]] 对列表中的元素进行排列,返回不重得的排列列表,结果是一个迭代器
<code class="hljs mathematica has-numbering">val nums = <span class="hljs-keyword">List</span>(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">3</span>) val combinations = nums.combinations(<span class="hljs-number">2</span>).toList //<span class="hljs-keyword">List</span>(<span class="hljs-keyword">List</span>(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>),<span class="hljs-keyword">List</span>(<span class="hljs-number">1</span>,<span class="hljs-number">3</span>)) val permutations = nums.permutations.toList // <span class="hljs-keyword">List</span>(<span class="hljs-keyword">List</span>(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">3</span>),<span class="hljs-keyword">List</span>(<span class="hljs-number">1</span>,<span class="hljs-number">3</span>,<span class="hljs-number">1</span>),<span class="hljs-keyword">List</span>(<span class="hljs-number">3</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>))</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li></ul>
19.zip, zipAll, zipWithIndex, unzip,unzip3
zip: zip[B](that: GenIterable[B]): List[(A, B)] 与另外一个列表进行拉链操作,将对应位置的元素组成一个pair,返回的列表长度为两个列表中短的那个
zipAll: zipAll[B](that: collection.Iterable[B], thisElem: A, thatElem: B): List[(A, B)] 与另外一个列表进行拉链操作,将对应位置的元素组成一个pair,若列表长度不一致,自身列表比较短的话使用thisElem进行填充,对方列表较短的话使用thatElem进行填充
zipWithIndex:zipWithIndex: List[(A, Int)] 将列表元素与其索引进行拉链操作,组成一个pair
unzip: unzip[A1, A2](implicit asPair: (A) ⇒ (A1, A2)): (List[A1], List[A2]) 解开拉链操作
unzip3: unzip3[A1, A2, A3](implicit asTriple: (A) ⇒ (A1, A2, A3)): (List[A1], List[A2], List[A3]) 3个元素的解拉链操作
<code class="hljs perl has-numbering">val alphabet = List(<span class="hljs-string">"A"</span>,B<span class="hljs-string">","</span>C<span class="hljs-string">") val nums = List(1,2) val zipped = alphabet zip nums // List(("</span>A<span class="hljs-string">",1),("</span>B<span class="hljs-string">",2)) val zippedAll = alphabet.zipAll(nums,"</span><span class="hljs-variable">*"</span>,-<span class="hljs-number">1</span>) // List((<span class="hljs-string">"A"</span>,<span class="hljs-number">1</span>),(<span class="hljs-string">"B"</span>,<span class="hljs-number">2</span>),(<span class="hljs-string">"C"</span>,-<span class="hljs-number">1</span>)) val zippedIndex = alphabet.zipWithIndex // List((<span class="hljs-string">"A"</span>,<span class="hljs-number">0</span>),(<span class="hljs-string">"B"</span>,<span class="hljs-number">1</span>),(<span class="hljs-string">"C"</span>,<span class="hljs-number">3</span>)) val (list1,list2) = zipped.unzip // list1 = List(<span class="hljs-string">"A"</span>,<span class="hljs-string">"B"</span>), list2 = List(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>) val (l1,l2,l3) = List((<span class="hljs-number">1</span>, <span class="hljs-string">"one"</span>, <span class="hljs-string">'1'</span>),(<span class="hljs-number">2</span>, <span class="hljs-string">"two"</span>, <span class="hljs-string">'2'</span>),(<span class="hljs-number">3</span>, <span class="hljs-string">"three"</span>, <span class="hljs-string">'3'</span>)).unzip3 // l1=List(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>),l2=List(<span class="hljs-string">"one"</span>,<span class="hljs-string">"two"</span>,<span class="hljs-string">"three"</span>),l3=List(<span class="hljs-string">'1'</span>,<span class="hljs-string">'2'</span>,<span class="hljs-string">'3'</span>)</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li></ul>
20.slice
slice(from: Int, until: Int): List[A] 提取列表中从位置from到位置until(不含该位置)的元素列表
<code class="hljs fsharp has-numbering"><span class="hljs-keyword">val</span> nums = List(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>) <span class="hljs-keyword">val</span> sliced = nums.slice(<span class="hljs-number">2</span>,<span class="hljs-number">4</span>) <span class="hljs-comment">//List(3,4)</span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li></ul>
21.sliding
sliding(size: Int, step: Int): Iterator[List[A]] 将列表按照固定大小size进行分组,步进为step,step默认为1,返回结果为迭代器
<code class="hljs mathematica has-numbering">val nums = <span class="hljs-keyword">List</span>(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">4</span>) val groupStep2 = nums.sliding(<span class="hljs-number">2</span>,<span class="hljs-number">2</span>).toList //<span class="hljs-keyword">List</span>(<span class="hljs-keyword">List</span>(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>),<span class="hljs-keyword">List</span>(<span class="hljs-number">2</span>,<span class="hljs-number">2</span>),<span class="hljs-keyword">List</span>(<span class="hljs-number">3</span>,<span class="hljs-number">3</span>),<span class="hljs-keyword">List</span>(<span class="hljs-number">4</span>,<span class="hljs-number">4</span>)) val groupStep1 = nums.sliding(<span class="hljs-number">2</span>).toList //<span class="hljs-keyword">List</span>(<span class="hljs-keyword">List</span>(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>),<span class="hljs-keyword">List</span>(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>),<span class="hljs-keyword">List</span>(<span class="hljs-number">2</span>,<span class="hljs-number">2</span>),<span class="hljs-keyword">List</span>(<span class="hljs-number">2</span>,<span class="hljs-number">3</span>),<span class="hljs-keyword">List</span>(<span class="hljs-number">3</span>,<span class="hljs-number">3</span>),<span class="hljs-keyword">List</span>(<span class="hljs-number">3</span>,<span class="hljs-number">4</span>),<span class="hljs-keyword">List</span>(<span class="hljs-number">4</span>,<span class="hljs-number">4</span>))</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li></ul>
22.updated
updated(index: Int, elem: A): List[A] 对列表中的某个元素进行更新操作
<code class="hljs fsharp has-numbering"><span class="hljs-keyword">val</span> nums = List(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">3</span>) <span class="hljs-keyword">val</span> fixed = nums.updated(<span class="hljs-number">3</span>,<span class="hljs-number">4</span>) <span class="hljs-comment">// List(1,2,3,4)</span></code>