2016-08-14 73 views
-1

想獲得分層turples的名單列表的列表清單,如何斯卡拉壓扁turples

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

你的代碼是無效的...'['和']'都是仿製藥。你的意思是'val folders = List((1,List((212,2asdad),(213,2asdas))),(2,List((112,asasd),(113,asasd))),... '或'val folders = List((1,(212,2asdad),(213,2asdas)),(2,(112,asasd),(113,asasd)),...' –

+0

@GáborBakos - I fixed感謝 –

回答

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型到你所需要的。

+0

謝謝你完美的作品,只是一些小修復 a.zipWithIndex.flatMap(x => x._1._2.map((x._2,x._1._1, _)))的foreach(的println) –

0

感謝@ Mika'il答案,這裏是解決方案

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