斯卡拉 - Tuple列表的两个列表 [英] Scala - Two Lists to Tuple List

查看:103
本文介绍了斯卡拉 - Tuple列表的两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

去年我对标准ML有很多经验,但在大约10个月内我没有做过任何真正的功能性编程。现在我正处于Scala潮流之中,在编写编译器时,我无法找到在标准ML中广泛使用的操作(虽然公平起见,这种方法可能不是库方法)。



基本上,我有两个列表:

 列表(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆