Scala中Zip相关的函数
在Scala中存在好几个Zip相关的函数,比如zip,zipAll,zipped 以及zipWithIndex等等。我们在代码中也经常看到这样的函数,这篇文章主要介绍一下这些函数的区别以及使用。
1、zip函数将传进来的两个参数中相应位置上的元素组成一个pair数组。如果其中一个参数元素比较长,那么多余的参数会被删掉。看下英文介绍吧:
Returns a list formed from this list and another iterable collection by combining corresponding elements in pairs. If one of the two collections is longer than the other, its remaining elements are ignored.
scala> val numbers = Seq(0, 1, 2, 3, 4)
numbers: Seq[Int] = List(0, 1, 2, 3, 4)
scala> val series = Seq(0, 1, 1, 2, 3)
series: Seq[Int] = List(0, 1, 1, 2, 3)
scala> numbers zip series
res24: Seq[(Int, Int)] = List((0,0), (1,1), (2,1), (3,2), (4,3), (5,5))
2、zipAll 函数和上面的zip函数类似,但是如果其中一个元素个数比较少,那么将用默认的元素填充。
The zipAll method generates an iterable of pairs of corresponding elements from xs and ys, where the shorter sequence is extended to match the longer one by appending elements x or y
/**
* User: 过往记忆
* Date: 14-12-17
* Time: 上午10:16
* bolg: http://www.iteblog.com
* 本文地址:http://www.iteblog.com/archives/1225
* 过往记忆博客,专注于hadoop、hive、spark、shark、flume的技术博客,大量的干货
* 过往记忆博客微信公共帐号:iteblog_hadoop
*/
scala> val xs = List(1, 2, 3)
xs: List[Int] = List(1, 2, 3)
scala> val ys = List('a', 'b')
ys: List[Char] = List(a, b)
scala> val zs = List("I", "II", "III", "IV")
zs: List1 = List(I, II, III, IV)
scala> val x = 0
x: Int = 0
scala> val y = '_'
y: Char = _
scala> val z = "_"
z: java.lang.String = _
scala> xs.zipAll(ys, x, y)
res30: List[(Int, Char)] = List((1,a), (2,b), (3,_))
scala> xs.zipAll(zs, x, z)
res31: List[(Int, java.lang.String)] = List((1,I), (2,II), (3,III), (0,IV))
3、zipped函数,这个不好翻译,自己看英文解释吧
The zipped method on tuples generalizes several common operations to work on multiple lists.
scala> val values = List.range(1, 5)
values: List[Int] = List(1, 2, 3, 4)
scala> (values, values).zipped toMap
res34: scala.collection.immutable.Map[Int,Int] = Map(1 -> 1, 2 -> 2, 3 -> 3, 4 -> 4)
scala> val sumOfSquares = (values, values).zipped map (_ * _) sum
sumOfSquares: Int = 30
4、zipWithIndex函数将元素和其所在的下表组成一个pair。
The zipWithIndex method pairs every element of a list with the position where it appears in the list.
scala> val series = Seq(0, 1, 1, 2, 3, 5, 8, 13)
series: Seq[Int] = List(0, 1, 1, 2, 3, 5, 8, 13)
scala> series.zipWithIndex
res35: Seq[(Int, Int)] = List((0,0), (1,1), (1,2), (2,3), (3,4), (5,5), (8,6), (13,7))
5、unzip函数可以将一个元组的列表转变成一个列表的元组
The unzip method changes back a list of tuples to a tuple of lists.
scala> val seriesIn = Seq(0, 1, 1, 2, 3, 5, 8, 13)
seriesIn: Seq[Int] = List(0, 1, 1, 2, 3, 5, 8, 13)
scala> val fibonacci = seriesIn.zipWithIndex
fibonacci: Seq[(Int, Int)] = List((0,0), (1,1), (1,2), (2,3), (3,4), (5,5), (8,6), (13,7))
scala> fibonacci.unzip
res46: (Seq[Int], Seq[Int]) = (List(0, 1, 1, 2, 3, 5, 8, 13),List(0, 1, 2, 3, 4, 5, 6, 7))
scala> val seriesOut = fibonacci.unzip _1
seriesOut: Seq[Int] = List(0, 1, 1, 2, 3, 5, 8, 13)
scala> val numbersOut = fibonacci.unzip _2
numbersOut: Seq[Int] = List(0, 1, 2, 3, 4, 5, 6, 7)
Scala中Zip相关的函数的更多相关文章
- scala中的高阶函数
版权申明:转载请注明出处. 文章来源:http://bigdataer.net/?p=332 排版乱?请移步原文获得更好阅读体验 1.scala中的函数 scala是一门面向对象和函数式编程相结合的语 ...
- Scala 中 构造函数,重载函数的执行顺序
在调试scala在线开发教程(http://www.imobilebbs.com/wordpress/archives/4911)的过程中看到了以下代码,但是这段代码无论怎么调试都无法成功. abst ...
- 第42讲:Scala中泛型类、泛型函数、泛型在Spark中的广泛应用
今天来了解下scala的泛型 先让我们看下这段代码 class Triple[F,S,T](val first:F,val second: S,val third: T) val triple = n ...
- Scala中 zip或者zipWithIndex的用法
问题:你要遍历一个有序集合,同时你又想访问一个循环计数器,但最重要的是你真的不需要手动创建这个计数器.解决方案: 使用zipWithIndex或者zip方法来自动地创建一个计数器,假设你有一个有 ...
- 2018-02-17 中文代码示例[译]Scala中创建隐式函数
前言: 学习Scala时, 顺便翻译一下自己有兴趣的文章. 代码中所有命名都中文化了(不是翻译). 比如原文用的是甜甜圈的例子. 原文: Scala Tutorial - Learn How To C ...
- Scala中柯里化函数
高阶函数转一阶函数: val add1 = (x: Int) => x + 5 def add2(x: Int)(y: Int) = x + y //传入一个参数转换为一阶函数 def add3 ...
- (数据科学学习手札48)Scala中的函数式编程
一.简介 Scala作为一门函数式编程与面向对象完美结合的语言,函数式编程部分也有其独到之处,本文就将针对Scala中关于函数式编程的一些常用基本内容进行介绍: 二.在Scala中定义函数 2.1 定 ...
- (转)scala中map与flatMap浅析
在函数式语言中,函数作为一等公民,可以在任何地方定义,在函数内或函数外,可以作为函数的参数和返回值,可以对函数进行组合.由于命令式编程语言也可以通过类似函数指针的方式来实现高阶函数,函数式的最主要的好 ...
- Scala中的构造器和高阶函数
构造器 在定义类时可以定义主构造器.主构造器可以同时声明字段. /** * 主构造器 * @author Administrator */ //在scala中,类和方法交织在一起 class Test ...
随机推荐
- DP:Sumsets(POJ 2229)
数的集合问题 题目大意:给定你一个整数m,你只能用2的k次幂来组合这个数,问你有多少种组合方式? 这一题一看,天啦太简单了,完全背包?是不是? 不过的确这一题可以用完全背包来想,但是交题绝对是TLE ...
- CodeForces - 417A(思维题)
Elimination Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit ...
- [Android Pro] ant 编译android工程
参考文章: http://blog.csdn.net/xyz_lmn/article/details/7268582?reload http://hubingforever.blog.163.com/ ...
- p188习题2
- 解决zabbix图中出现中文乱码问题 图中的中文会变成方块
[root@node03 src]# wget http://down1.chinaunix.net/distfiles/ttf-arphic-uming_0.0.20050501-1.tar.gz ...
- linux下php增加curl扩展,生成curl.so文件
进入php源代码目录 cd /php5.6.9/ext/curl 执行生成so文件编译模式 /usr/local/php/bin/phpize 编译curl扩展 ./configure --with- ...
- 一个可能有用的封闭PGSQL操作的PYTHON函数
URL: http://www.linuxyw.com/517.html 一般操作: import psycopg2 连接数据库 conn = psycopg2.connect(database=db ...
- mac平台下面nodejs环境搭配
方法一(pkg方式:下载链接http://nodejs.org/dist/v0.12.4/node-v0.12.4.pkg): 官网:https://nodejs.org/download/ 例子: ...
- C# WPF 之 遍历子控件
/// <summary> /// 检查非空字段 /// </summary> /// <param name="IsOk"></para ...
- poj 2486( 树形dp)
题目链接:http://poj.org/problem?id=2486 思路:经典的树形dp,想了好久的状态转移.dp[i][j][0]表示从i出发走了j步最后没有回到i,dp[i][j][1]表示从 ...