给出以下列表:
val l = List(List(1, 2, 3), List(4, 5), List(6, 7, 8))
如果我尝试转置它,Scala将抛出以下错误:
scala> List.transpose(l)
java.util.NoSuchElementException: head of empty list
at scala.Nil$.head(List.scala:1365)
at scala.Nil$.head(List.scala:1362)
at scala.List$$anonfun$transpose$1.apply(List.scala:417)
at scala.List$$anonfun$transpose$1.apply(List.scala:417)
at scala.List.map(List.scala:812)
at scala.List$.transpose(List.scala:417)
at .<init>(<console>:6)
at .<clinit>(<console>)
at RequestResult...
这是因为List.transpose
假定等长列表,因此使用head
方法:
def transpose[A](xss: List[List[A]]): List[List[A]] = {
val buf = new ListBuffer[List[A]]
var yss = xss
while (!yss.head.isEmpty) {
buf += (yss map (_.head))
yss = (yss map (_.tail))
}
buf.toList
}
我想得到以下内容:
List(List(1, 4, 6), List(2, 5, 7), List(3, 8))
编写我自己的transpose
版本是最好的方法吗?这就是我想出的:
def myTranspose[A](xss: List[List[A]]): List[List[A]] = {
val buf = new ListBuffer[List[A]]
var yss = xss
while (!yss.head.isEmpty) {
buf += (yss filter (!_.isEmpty) map (_.head))
yss = (yss filter (!_.isEmpty) map (_.tail))
}
buf.toList
}
更新:我有兴趣比较这里提供的不同解决方案的速度,所以我把以下小基准测试放在一起:
import scala.testing.Benchmark
import scala.collection.mutable.ListBuffer
trait Transpose extends Benchmark {
def transpose[Int](xss: List[List[Int]]): List[List[Int]] = Nil
val list: List[List[Int]] = List(List(1,2,3), Nil, List(4,5,99,100), List(6,7,8))
def run = {
val l = transpose(list)
println(l)
l
}
}
object PRTranspose extends Transpose {
override def transpose[Int](xss: List[List[Int]]): List[List[Int]] = {
val buf = new ListBuffer[List[Int]]
var yss = xss
while (!yss.head.isEmpty) {
buf += (yss filter (!_.isEmpty) map (_.head))
yss = (yss filter (!_.isEmpty) map (_.tail))
}
buf.toList
}
}
object ACTranspose extends Transpose {
override def transpose[Int](xss: List[List[Int]]): List[List[Int]] = {
val b = new ListBuffer[List[Int]]
var y = xss filter (!_.isEmpty)
while (!y.isEmpty) {
b += y map (_.head)
y = y map (_.tail) filter (!_.isEmpty)
}
b.toList
}
}
object ETranspose extends Transpose {
override def transpose[Int](xss: List[List[Int]]): List[List[Int]] = xss.filter(!_.isEmpty) match {
case Nil => Nil
case ys: List[List[Int]] => ys.map{ _.head }::transpose(ys.map{ _.tail })
}
}
我的命令是:
scala PFTranspose 5 out.log
scala ACTranspose 5 out.log
scala ETranspose 5 out.log
我的结果是:
PRTranspose$ 10 0 1 1 0
ACTranspose$ 9 2 0 0 0
ETranspose$ 9 3 2 3 1
答案 0 :(得分:9)
这个怎么样:
scala> def transpose[A](xs: List[List[A]]): List[List[A]] = xs.filter(_.nonEmpty) match {
| case Nil => Nil
| case ys: List[List[A]] => ys.map{ _.head }::transpose(ys.map{ _.tail })
| }
warning: there were unchecked warnings; re-run with -unchecked for details
transpose: [A](xs: List[List[A]])List[List[A]]
scala> val ls = List(List(1, 2, 3), List(4, 5), List(6, 7, 8))
ls: List[List[Int]] = List(List(1, 2, 3), List(4, 5), List(6, 7, 8))
scala> transpose(ls)
res0: List[List[Int]] = List(List(1, 4, 6), List(2, 5, 7), List(3, 8))
scala> val xs = List(List(1,2,3), List(4,5,99,100), List(6,7,8))
xs: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 99, 100), List(6, 7, 8))
scala> transpose(xs)
res1: List[List[Int]] = List(List(1, 4, 6), List(2, 5, 7), List(3, 99, 8), List(100))
答案 1 :(得分:4)
我怀疑在“非矩形”列表列表中没有定义转置的原因是因为数学上转置操作仅在“矩形结构”上很好地定义。转置操作的理想特性是转置(转置(x))== x。在非矩形列表列表中对转置操作的推广不是这种情况。
另外,请查看Transposing arbitrary collection-of-collections in Scala上的帖子,并考虑为非矩形馆藏集合进行此操作。最终会得到数学上不一致的定义,而不用单独的实现。
我同意特殊的“转置”操作通常很有用,但我也认为它们不应该在标准库中提供,因为它们的精确定义可能存在混淆。
答案 2 :(得分:3)
我不知道(并且无法想象 - 这是不是有点奇怪?![见评论中的讨论])一个库函数,但我可以稍微抛光一下代码:
scala> def transpose(x: List[List[Int]]): List[List[Int]] = {
| val b = new ListBuffer[List[Int]]
| var y = x filter (!_.isEmpty)
| while (!y.isEmpty) {
| b += y map (_.head)
| y = y map (_.tail) filter (!_.isEmpty)
| }
| b.toList
| }
答案 3 :(得分:0)
这可能是最干净的:
def transpose[T](l: List[List[T]]): List[List[T]] =
l.flatMap(_.headOption) match {
case Nil => Nil
case head => head :: transpose(l.map(_.drop(1)))
}
或更高效的修改版本:
def transpose[T](l: List[List[T]]): List[List[T]] =
l.flatMap(_.headOption) match {
case Nil => Nil
case head => head :: transpose(l.collect { case _ :: tail => tail })
}
答案 4 :(得分:0)
使用Scala标准Api的这种单线程如何:
((l map (_.toArray)) toArray).transpose map (_.toList) toList
这可以完成工作,并且是O(N*M)
,其中N
是包装器列表的长度,M
是包装器列表中最长列表的长度。