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 空列表