如何合并两个 Seq[String], Seq[Double] 到 Seq[(String,Double)]

How to merge two Seq[String], Seq[Double] to Seq[(String,Double)]

我有两个Seq。 1 个 Seq[String],另一个 Seq[(String,Double)]

a -> ["a","b","c"]b-> [1,2,3]

我想将输出创建为

[("a",1),("b",2),("c",3)]

我有密码 a.zip(b) 实际上是创建这两个元素的序列而不是创建映射

任何人都可以建议如何在 Scala 中做到这一点吗?

您只需要 .toMap 即可将 List[Tuple[String, Int]] 转换为 Map[String, Int]

scala> val seq1 = List("a", "b", "c")
seq1: List[String] = List(a, b, c)

scala> val seq2 = List(1, 2, 3)
seq2: List[Int] = List(1, 2, 3)

scala> seq1.zip(seq2)
res0: List[(String, Int)] = List((a,1), (b,2), (c,3))

scala> seq1.zip(seq2).toMap
res1: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2, c -> 3)

另见

如何使用A的值作为映射中的键将Seq[A]转换为Map[Int, A]?