list 如何在 Scala 中展平不同类型的列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1737452/
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:25:19  来源:igfitidea点击:

How to flatten a List of different types in Scala?

listscalanestedflatten

提问by clau

I have 4 elements:List[List[Object]](Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]]

我有 4 个元素:(List[List[Object]]每个元素中的对象都不同)我想压缩它们以便我可以有一个List[List[obj1],List[obj2],List[obj3],List[obj4]]

I tried to zip them and I obtained a nested list that I can't apply flatten to because it says: no implicit argument matching parameter type.

我试图压缩它们,但我获得了一个嵌套列表,我无法将其应用展平,因为它说:没有隐式参数匹配参数类型。

How can I solve this? should I try another way or is there any way to make the flatten work?

我该如何解决这个问题?我应该尝试另一种方式还是有什么方法可以使展平工作?

I'm kinda new to scala so it may be a dumb question :D Thanks in advance! clau

我对 Scala 有点陌生,所以这可能是一个愚蠢的问题 :D 提前致谢!克劳

回答by Jatin

For One Nested List: flattenwill do:

对于一个嵌套列表flatten会做:

scala> List(List(1), List(2), List(3)).flatten
res4: List[Int] = List(1, 2, 3)

scala> List(List(List(1)), List(List(2)), List(List(3))).flatten
res5: List[List[Int]] = List(List(1), List(2), List(3))

For multiple Nested Liststhen you can:

对于多个嵌套列表,您可以:

def flatten(ls: List[Any]): List[Any] = ls flatMap {
  case i: List[_] => flatten(i)
  case e => List(e)
}

val k = List(1, List(2, 3), List(List(List(List(4)), List(5)), List(6, 7)), 8)
flatten(k)

It prints List[Any] = List(1, 2, 3, 4, 5, 6, 7, 8)

它打印 List[Any] = List(1, 2, 3, 4, 5, 6, 7, 8)

回答by Ben James

Before Scala 2.9

在 Scala 2.9 之前

From the error you pasted, it looks like you're trying to call the flatteninstance method of the nested list itself. That requires an implicit conversion to make something of type Iterableout of whatever types the List contains. In your case, it looks like the compiler can't find one.

从您粘贴的错误来看,您似乎正在尝试调用flatten嵌套列表本身的实例方法。这需要隐式转换以Iterable从 List 包含的任何类型中生成某种类型。在您的情况下,编译器似乎找不到一个。

Use flattenfrom the Listsingleton object, which doesn't require that implicit parameter:

使用flattenList单独的对象,它不需要隐含参数:

scala> val foo = List(List(1), List("a"), List(2.3))
foo: List[List[Any]] = List(List(1), List(a), List(2.3))

scala> List.flatten(foo)
res1: List[Any] = List(1, a, 2.3)

After Scala 2.9

在 Scala 2.9 之后

Just use foo.flatten.

只需使用foo.flatten.

回答by Daniel C. Sobral

The question is very vague. You should plain pastewhat you have, instead of trying to describe it. It would make everyone's (including your's) life much easier.

这个问题很模糊。你应该简单地粘贴你所拥有的,而不是试图描述它。这将使每个人(包括您)的生活变得更加轻松。

The code below is one example based on an assumption of what you have.

下面的代码是一个基于您所拥有的假设的示例。

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

scala> List(List(2))
res1: List[List[Int]] = List(List(2))

scala> List(List(3))
res2: List[List[Int]] = List(List(3))

scala> List(List(4))
res3: List[List[Int]] = List(List(4))

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

回答by Rahul Kulhari

In scala 2.10.2

在 Scala 2.10.2

scala> val foo = List(List(1), List("a"), List(2.3))
foo: List[List[Any]] = List(List(1), List(a), List(2.3))

scala> foo.flatten
res0: List[Any] = List(1, 2, a, 2.3)

working fine but

工作正常但是

if you run like

如果你像

scala>  val foo = List(List(1,2), 2, List(2.3))
foo: List[Any] = List(List(1, 2), 2, List(2.3))

scala> foo.flatten
<console>:9: error: No implicit view available from Any => scala.collection.GenTraversableOnce[B].
              foo.flatten

for that i write function

为此我写函数

scala> def flat(ls: List[Any]): List[Any]= ls flatten {
     |   case t: List[Any] =>  flat(t)
     |   case c => List(c)
     | }   
flat: (ls: List[Any])List[Any]

scala> flat(List(List(1,2),2,List(2.3)))
res2: List[Any] = List(1, 2, 2, 2.3)

回答by Shaun

It helps if we have an example. Your code should look something like:

如果我们有一个例子,它会有所帮助。您的代码应该类似于:

val f = List(1, 2)
val s = List(3, 4)
val top = List(f, s)

List.flatten(top) // returns List(1, 2, 3, 4)

回答by Ken Bloom

You can only zip two lists at a time with list1 zip list2, and the type signature for the return values is List[(A,B)]not List[List[obj1],List[obj2],List[obj3],List[obj4]]

一次只能压缩两个列表list1 zip list2,并且返回值的类型签名List[(A,B)]不是List[List[obj1],List[obj2],List[obj3],List[obj4]]

回答by elm

Consider List.concat, for instance

考虑List.concat,例如

List.concat(List(1), List(2,22), List(3))    // delivers List(1, 2, 22, 3)