Scala - 将列表列表转换为单个列表:List[List[A]] 到 List[A] [英] Scala - convert List of Lists into a single List: List[List[A]] to List[A]

查看:37
本文介绍了Scala - 将列表列表转换为单个列表:List[List[A]] 到 List[A]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Scala (2.9) 中转换列表列表的最佳方法是什么?

What's the best way to convert a List of Lists in scala (2.9)?

我有一个清单:

List[List[A]]

我想转换成

List[A]

如何递归实现?或者还有其他更好的方法吗?

How can that be achieved recursively? Or is there any other better way?

推荐答案

List 有 flatten 方法.为什么不使用它?

List has the flatten method. Why not use it?

List(List(1,2), List(3,4)).flatten
> List(1,2,3,4)

这篇关于Scala - 将列表列表转换为单个列表:List[List[A]] 到 List[A]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆