【问题标题】:how to unpack list(list()) in scala [duplicate]如何在scala中解压list(list())[重复]
【发布时间】:2019-07-05 20:27:52
【问题描述】:

嗨,我是 scala 的初学者, 我要解压

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")

我们怎样才能实现?

谢谢!

【问题讨论】:

  • 使用flatten方法

标签: scala nested-lists flatten


【解决方案1】:

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

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)

【讨论】:

    猜你喜欢
    • 2015-03-18
    • 1970-01-01
    • 2018-02-20
    • 2020-01-11
    • 1970-01-01
    • 2017-07-14
    • 2013-09-01
    • 2016-07-02
    • 2019-07-10
    相关资源
    最近更新 更多