如何使用scala从另一个列表中插入列表中的元素?

时间:2015-01-10 09:10:15

标签: scala

我有两个清单 -

A = (("192.168.1.1","private","Linux_server","str1"), 
("192.168.1.2","private","Linux_server","str2"))

B = ("A","B")

我想要关注输出

outputList = (("192.168.1.1","private","Linux_server","str1", "A"), 
("192.168.1.2","private","Linux_server","str2","B"))

我想将第二个列表元素作为列表序列插入到第一个列表中。

两个列表大小始终相同。

如何使用scala获得以上输出?

1 个答案:

答案 0 :(得分:3)

答案简短:

A = (A zip B).map({ case (x, y) => x :+ y })

某些编译代码更明确:

val a = List(
  List("192.168.1.1", "private", "Linux_server", "str1"), 
  List("192.168.1.2", "private", "Linux_server", "str2")
)

val b = List("A", "B")

val c = List(
  List("192.168.1.1", "private", "Linux_server", "str1", "A"), 
  List("192.168.1.2", "private", "Linux_server", "str2", "B")
)

assert((a zip b).map({ case (x, y) => x :+ y }) == c)