且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将列表分组为 Scala 中的元组分组项?

更新时间:2023-01-08 15:58:47

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.