[英]How to merge two Seq[String], Seq[Double] to Seq[(String,Double)]
I have two Seq. 我有两个序列。 1 has Seq[String]
and another has Seq[(String,Double)]
1个具有Seq[String]
,另一个具有Seq[(String,Double)]
a -> ["a","b","c"]
and b-> [1,2,3]
a -> ["a","b","c"]
和b-> [1,2,3]
I want to create output as 我想创建输出为
[("a",1),("b",2),("c",3)]
I have a code a.zip(b)
is actually creating a seq of those two elements instead of creating a map 我有一个代码a.zip(b)
实际上是创建这两个元素的a.zip(b)
而不是创建地图
Can anyone suggest how to do that in scala? 谁能建议如何在Scala中做到这一点?
you simply need .toMap
so that you can transform List[Tuple[String, Int]]
to Map[String, Int]
您只需要.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)
How to convert a Seq[A] to a Map[Int, A] using a value of A as the key in the map? 如何使用A的值作为映射中的键将Seq [A]转换为Map [Int,A]?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.