我试图弄清楚列表中的列表,
val folders =List( (1, (List(212, 2asdad), List(213, 2asdas))),
(2, (List(112, asasd), List(113, asasd6))),
...
)
val ouput = folders.zipWithIndex.map(sc => sc._1._2.map((sc._2, sc._1._1, _))).foreach(println)
//output = List((1, 212, 2asdad), (1, 213, 2asdas )),
// List((2, 112, asasd), (2, 113, asasd6 )),
但我有兴趣获得像这样的平面列表
//output = List((1, 212, 2asdad), (1, 213, 2asdas ),
// (2, 112, asasd), (2, 113, asasd6 )),
知道如何解决这个问题吗?
由于
答案 0 :(得分:1)
你想要的是采用List[(Int, List[(Int, Symbol)])]
类型并将其转换为List[(Int, Int, Symbol)]
类型。如果您需要,那么以下内容将起作用:
scala> val a = List((1,List((2,'a), (3,'b))),(2,List((3,'c), (4,'d))))
a: List[(Int, List[(Int, Symbol)])] = List((1,List((2,'a), (3,'b))), (2,List((3,'c), (4,'d))))
scala> a.flatMap(x => x._2.map(y => (x._1, y._1, y._2)))
res18: List[(Int, Int, Symbol)] = List((1,2,'a), (1,3,'b), (2,3,'c), (2,4,'d))
只需将Symbol
类型更改为您需要的类型。
答案 1 :(得分:0)
感谢@ Mika'我的回答,这是解决方案
val folders =List( (1, (List(212, 2asdad), List(213, 2asdas))),
(2, (List(112, asasd), List(113, asasd6))),
...
)
folders.zipWithIndex.flatMap(x => x._1._2.map((x._2, x._1._1,_))).foreach(println)
输出
List((1, 212,2asdad), (1, 213, 2asdas), (2, 112, asasd), (2,113,asas6))