如何在scala中解压list(list())

时间:2019-02-12 08:38:46

标签: scala nested-lists flatten

嗨,我是斯卡拉的新手, 我想打开

List(List("hello=10","hi=21","there=13","bro=44","family=44","technology=35","hi=20","hello=100","hi=21","there=13","bro=44","family=44","technology=35","hi=21","there=13","bro=44","family=44","family=44"))

变成类似

List("hello=10","hi=21","there=13","bro=44","family=44","technology=35","hi=20","hello=100","hi=21","there=13","bro=44","family=44","technology=35","hi=21","there=13","bro=44","family=44","family=44")

我们如何达到目标?

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用列表的内置方法展平。

scala> val list = List(List("hello=10","hi=21","there=13","bro=44","family=44","technology=35","hi=20","hello=100","hi=21","there=13","bro=44","family=44","technology=35","hi=21","there=13","bro=44","family=44","family=44"))
list: List[List[String]] = List(List(hello=10, hi=21, there=13, bro=44, family=44, technology=35, hi=20, hello=100, hi=21, there=13, bro=44, family=44, technology=35, hi=21, there=13, bro=44, family=44, family=44))

scala> list.flatten
res0: List[String] = List(hello=10, hi=21, there=13, bro=44, family=44, technology=35, hi=20, hello=100, hi=21, there=13, bro=44, family=44, technology=35, hi=21, there=13, bro=44, family=44, family=44)