如何使用scala组合两个列表?
发布时间:2020-12-16 18:05:25 所属栏目:安全 来源:网络整理
导读:我有以下列表 – val A = List(("A","B","C","D"),("A1","B1","C1","D1"),("A2","B2","C2","D2")) 和 val B = List(("P","Q","R","S","T"),("P1","Q1","R1","S1","T1"),("P2","Q2","R2","S2","T2"),("P3","Q3","R3","S3","T3")) 我想将列表A的第一个元素与列
我有以下列表 –
val A = List(("A","B","C","D"),("A1","B1","C1","D1"),("A2","B2","C2","D2")) 和 val B = List(("P","Q","R","S","T"),("P1","Q1","R1","S1","T1"),("P2","Q2","R2","S2","T2"),("P3","Q3","R3","S3","T3")) 我想将列表A的第一个元素与列表B的第一个元素合并,依此类推. 输出如下 val combineList = List(("A","D","P","D1","P1","D2","P2","T2")) 解决方法
如果你可以使用无形,那么你可以简单地做
scala> import shapeless.syntax.std.tuple._ scala> A.zip(B).map{case(a,b) => a ++ b} res1: List[(String,String,String)] = List((A,B,C,D,P,Q,R,S,T),(A1,B1,C1,D1,P1,Q1,R1,S1,T1),(A2,B2,C2,D2,P2,Q2,R2,S2,T2)) 它将适用于任意大小的元组. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |