如何将列表分组为 Scala 中的元组分组项? [英] How can I group the a list into tuple grouped items in Scala?
本文介绍了如何将列表分组为 Scala 中的元组分组项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如我如何转换
val list=(1 to 10).toList
进入
List((1,2),(3,4),(5,6),(7,8),(9,10))
推荐答案
List 类可以使用 grouped
方法:http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.List
You may use grouped
method for the List class: http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.List
list.grouped(2).toList.collect { case a :: b :: Nil => (a,b) }
res1: List[(Int, Int)] = List((1,2), (3,4), (5,6), (7,8), (9,10))
collect
用于将列表列表转换为元组列表.
collect
is used to convert list of lists to list of tuples.
这篇关于如何将列表分组为 Scala 中的元组分组项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文