scala 空列表_如何在Scala中展平列表列表?

scala 空列表

Flattening of List is converting a list of multiple List into a single List. To flatten List of List in Scala we will use the flatten method.

扁平化列表是将多个列表的列表转换为单个列表。 为了在Scala中扁平化List列表,我们将使用flatten方法。

Syntax:

句法:

    val newList = list.flatten

Program to flatten a list of list with numerical values

程序用数值拼合列表列表

object MyClass {
    def main(args: Array[String]) {
        val ListofList = List(List(214, 56), List(6, 12, 34, 98))
        
        println("List of List: " + ListofList)
        
        println("Flattening List of List ")
        
        val list = ListofList.flatten
        
        println("Flattened List: " + list)
    }
}

Output

输出量

List of List: List(List(214, 56), List(6, 12, 34, 98))
Flattening List of List 
Flattened List: List(214, 56, 6, 12, 34, 98)

The same flatten method can also be applied to convert sequence of sequence to a single sequence(Array, list, Vector, etc.).

同样的展平方法也可以应用于将序列序列转换为单个序列( 数组 ,列表,向量等)。

You can convert list of lists to list of type strings in two ways.

您可以通过两种方式将列表列表转换为类型字符串列表

Let's explore how to?

让我们探索如何?

Program:

程序:

object MyClass {
    def main(args: Array[String]) {
        val ListofList = List(List("Include", "Help"), List("Programming", "Tutorial"))

        println("List of List: " + ListofList)

        println("Flattening List of List ")

        val list = ListofList.flatten

        println("Flattened List: " + list)
    }
}

Output

输出量

List of List: List(List(Include, Help), List(Programming, Tutorial))
Flattening List of List 
Flattened List: List(Include, Help, Programming, Tutorial)

Another way could split the list of list into character lists.

另一种方法可以将列表列表拆分为字符列表。

Program:

程序:

object MyClass {
    def main(args: Array[String]) {
        val ListofList = List("Include", "Help")

        println("List of List: " + ListofList)

        println("Flattening List of List ")

        val list = ListofList.flatten

        println("Flattened List: " + list)
    }
}

Output

输出量

List of List: List(Include, Help)
Flattening List of List 
Flattened List: List(I, n, c, l, u, d, e, H, e, l, p)


翻译自: https://www.includehelp.com/scala/how-to-flatten-a-list-of-list-in-scala.aspx

scala 空列表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值