我有以下两个清单 -
List(("ABC",1,10),("PQR",1,10))
List((1,"abc",3940903,0.0),(2,"pqr",1234,3.0))
我想要关注输出
List(("ABC",1,10,1,"abc",3940903,0.0),("PQR",1,10,2,"pqr",1234,3.0)
我尝试了concat,:::但是没有为我工作。
如何使用scala获得以上输出?
答案 0 :(得分:5)
您无法直接在Scala中合并元组。有两种方法可以实现它
val A = List(("ABC", 1, 10), ("PQR", 1, 10))
val B = List((1, "abc", 3940903, 0.0), (2, "pqr", 1234, 3.0))
val zippedList = A zip B
import shapeless.syntax.std.tuple._
zippedList.map { case (a, b) => a ++ b }
//List((ABC,1,10,1,abc,3940903,0.0), (PQR,1,10,2,pqr,1234,3.0))
此方法适用于任意大小的元组
不使用外部库
zippedList.map { case ((a,b,c), (p,q,r,s)) => (a,b,c,p,q,r,s) }
//List((ABC,1,10,1,abc,3940903,0.0), (PQR,1,10,2,pqr,1234,3.0))
元组应该有固定的元素才能工作