Scala - 将列表列表转换为单个列表:List[List[A]] 到 List[A]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12600863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:53:51  来源:igfitidea点击:

Scala - convert List of Lists into a single List: List[List[A]] to List[A]

scalalistrecursionflatten

提问by A Far

What's the best way to convert a List of Lists in scala (2.9)?

在 Scala (2.9) 中转换列表列表的最佳方法是什么?

I have a list:

我有一个清单:

List[List[A]]

which I want to convert into

我想转换成

List[A]

How can that be achieved recursively? Or is there any other better way?

如何递归实现?或者还有其他更好的方法吗?

回答by Jan

List has the flatten method. Why not use it?

List 有 flatten 方法。为什么不使用它?

List(List(1,2), List(3,4)).flatten
> List(1,2,3,4)

回答by Dave Griffith

.flatten is obviously the easiest way, but for completeness you should also know about flatMap

.flatten 显然是最简单的方法,但为了完整起见,您还应该了解 flatMap

 val l = List(List(1, 2), List(3, 4))
 println(l.flatMap(identity))

and the for-comprehension equivalent

和 for-comprehension 等价物

 println(for (list <- l; x <- list) yield x)

flatten is obviously a special case of flatMap, which can do so much more.

flatten 显然是 flatMap 的一个特例,它可以做更多的事情。

回答by Brian Agnew

Given the above example, I'm not sure you need recursion. Looks like you want List.flatteninstead.

鉴于上面的例子,我不确定你是否需要递归。看起来你想要List.flatten

e.g.

例如

scala> List(1,2,3)
res0: List[Int] = List(1, 2, 3)

scala> List(4,5,6)
res1: List[Int] = List(4, 5, 6)

scala> List(res0,res1)
res2: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6)) 

scala> res2.flatten
res3: List[Int] = List(1, 2, 3, 4, 5, 6)

回答by Maroun

If your structure can be further nested, like:

如果您的结构可以进一步嵌套,例如:

List(List(1, 2, 3, 4, List(5, 6, List(7, 8))))

This function should give you the desire result:

这个函数应该给你想要的结果:

def f[U](l: List[U]): List[U] = l match {
  case Nil => Nil
  case (x: List[U]) :: tail => f(x) ::: f(tail)
  case x :: tail => x :: f(tail)
}

回答by Robin

If you want to use flatmap, here is the the way

如果你想使用 flatmap,这里是方法

Suppose that you have a List of List[Int] named ll, and you want to flat it to List, many people already gives you the answers, such as flatten, that's the easy way. I assume that you are asking for using flatmap method. If it is the case, here is the way

假设你有一个名为 ll 的 List[Int] 的 List,你想把它扁平化到 List,很多人已经给你答案了,比如 flatten,这个方法很简单。我假设您要求使用 flatmap 方法。如果是这样,这里是方法

ll.flatMap(_.map(o=>o))

回答by Pawe? Janik

You don't need recursion but you can use it if you want:

您不需要递归,但可以根据需要使用它:

def flatten[A](list: List[List[A]]):List[A] = 
  if (list.length==0) List[A]() 
  else list.head ++ flatten(list.tail)

This works like flatten method build into List. Example:

这就像将扁平方法构建到列表中一样。例子:

scala> flatten(List(List(1,2), List(3,4)))
res0: List[Int] = List(1, 2, 3, 4)