问题描述:

This question already has an answer here:

  • Can I zip more than two lists together in Scala?

    10 answers

网友答案:

This works, under the assumption that the outer list has exacly two inner lists.

List(List(0,0,0), List(1, 1, 1)) match {
  case l1 :: l2 :: Nil => l1 zip l2
  case x => ??? // up to you
}

In order to take into account the possibility of failure, you can wrap the result into an Option.

val x = List(List(0,0,0), List(1, 1, 1))
val res: Option[List[Int]] = x match {
  case l1 :: l2 :: Nil => Some(l1 zip l2)
  case x => None
}
相关阅读:
Top