Scala - 两个列表到元组列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16423398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala - Two Lists to Tuple List
提问by GJK
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).
去年我在标准 ML 方面有相当多的经验,但我已经有大约 10 个月没有做过任何真正的函数式编程了。现在我已经加入了 Scala 的潮流,我在编写编译器时很难找到我在标准 ML 中广泛使用的操作(尽管公平地说,这个方法可能不是一个库方法)。
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.)
我可以使用标准的 Scala 函数来获得这个结果吗?(我认为我们在标准 ML 中将其称为 zip 函数,但是当我搜索 Scala zip 函数时,这似乎指的是不同的东西。)
回答by om-nom-nom
You're right you can use 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))

