斯卡拉 - Tuple列表的两个列表 [英] Scala - Two Lists to Tuple List
问题描述
基本上,我有两个列表:
列表(a,b ,c)
List(1,2,3)
我想一个操作会给我一个这样的元组列表:
List((a,1),(b ,2),(c,3))
是否有标准的Scala函数?用来获得这个结果? (我认为我们把它称为标准ML中的zip函数,但是当我搜索Scala zip函数时,它似乎指的是不同的东西)。 解决方案你是对的,你可以使用zip:
val a = List(a,b ,c)
// a:List [String] = List(a,b,c)
val b = List(1,2,3)
// b:List [Int] = List(1,2,3)
a zip b // beautified a.zip(b)
// res0:List [(String,Int )] = List((a,1),(b,2),(c,3))
Last year I had quite a bit of experience with standard ML, but I haven't done any real functional programming in about 10 months. Now that I'm on the Scala bandwagon, I'm having trouble finding an operation which I used extensively in standard ML when writing a compiler (although to be fair, this method may not have been a library method).
Basically, I have two lists:
List("a","b","c")
List(1,2,3)
And I want an operation that will give me a list of tuples like this:
List(("a",1), ("b",2), ("c",3))
Is there a standard Scala function I can use to get this result? (I think we called it a zip function in standard ML, but that seems to refer to something different when I was searching for Scala zip functions.)
You're right you can use zip:
val a = List("a","b","c")
// a: List[String] = List(a, b, c)
val b = List(1,2,3)
// b: List[Int] = List(1, 2, 3)
a zip b // beautified a.zip(b)
//res0: List[(String, Int)] = List((a,1), (b,2), (c,3))
这篇关于斯卡拉 - Tuple列表的两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!