首页 / SCALA / Scala中Zip相关的函数
Scala中Zip相关的函数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Scala中Zip相关的函数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3069字,纯文字阅读大概需要5分钟。
内容图文
在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)
原文:http://www.cnblogs.com/suanec/p/4810686.html
内容总结
以上是互联网集市为您收集整理的Scala中Zip相关的函数全部内容,希望文章能够帮你解决Scala中Zip相关的函数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。