加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何将Scala列表成对?

发布时间:2020-12-16 09:40:25 所属栏目:安全 来源:网络整理
导读:我试图将列表(1,2,3,4)的Scala列表分成成对(1,2)(2,3)(3,4),这是一个简单的方法? 解决方法 val xs = List(1,4)xs zip xs.tail // res1: List[(Int,Int)] = List((1,2),(2,3),(3,4)) 正如文档所说,zip Returns a list formed from this list and another i
我试图将列表(1,2,3,4)的Scala列表分成成对(1,2)(2,3)(3,4),这是一个简单的方法?

解决方法

val xs = List(1,4)
xs zip xs.tail
  // res1: List[(Int,Int)] = List((1,2),(2,3),(3,4))

正如文档所说,zip

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.

列表(‘a,’b,’c,’d)用List(‘x,’y,’z)压缩是List((‘a,’x),(‘b,’y), ,’z)),最后的第一个被忽略。

从你的例子中,List(1,4)的尾部是List(2,4),所以你可以看到这些zip如何成对。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读