Scala:按顺序从列表元素创建元组列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31800032/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 07:26:27  来源:igfitidea点击:

Scala: Creating a list of tuples from list elements sequentially

listscalafunctional-programmingimmutability

提问by rivu

I am very new to Scala so this question may be very naive.

我对 Scala 很陌生,所以这个问题可能很幼稚。

I have a list like this List[Int] = List(0, 3, 6, 12, 14, 15, 16, 17). I am trying to create a list like this [(0,3),(3,6),(6,12)..]and so on. So far this is what I have tried:

我有一个这样的清单List[Int] = List(0, 3, 6, 12, 14, 15, 16, 17)。我正在尝试创建一个这样的列表[(0,3),(3,6),(6,12)..]等等。到目前为止,这是我尝试过的:

val l1= List(0, 3, 6, 12, 14, 15, 16, 17)
var l2=scala.collection.mutable.ListBuffer[(Int,Int)]()
l1.zipWithIndex.slice(0,l1.length-1).foreach(x=>{val newval=(x._1,l1(x._2+1)); l2+=newval})

Two questions here:

这里有两个问题:

  1. If I don't use val newval, i.e. try to do l1.zipWithIndex.slice(0,l1.length-1).foreach(x=>l2+=(x._1,l1(x._2+1))), the compiler says: <console>:10: error: type mismatch; found : Int required: (Int, Int) l1.zipWithIndex.slice(0,l1.length-1).foreach(x=>l2+=(x._1,l1(x._2+1))). Why is that?
  2. What would a way to do it without the mutable listbuffer?
  1. 如果我不使用val newval,即尝试做l1.zipWithIndex.slice(0,l1.length-1).foreach(x=>l2+=(x._1,l1(x._2+1))),编译器会说: <console>:10: error: type mismatch; found : Int required: (Int, Int) l1.zipWithIndex.slice(0,l1.length-1).foreach(x=>l2+=(x._1,l1(x._2+1)))。这是为什么?
  2. 如果没有可变列表缓冲区,有什么方法可以做到?

回答by Michael Zajac

  1. +=is a method on the ListBufferl2that accepts repeated parameters. That means when you do something like this:

    scala> var l2 = scala.collection.mutable.ListBuffer[(Int, Int)]()
    l2: scala.collection.mutable.ListBuffer[(Int, Int)] = ListBuffer()
    
    scala> l2 += (1, 2)
    <console>:9: error: type mismatch;
     found   : Int(1)
     required: (Int, Int)
                  l2 += (1, 2)
    
  1. +=是一个ListBufferl2接受重复参数的方法。这意味着当你做这样的事情时:

    scala> var l2 = scala.collection.mutable.ListBuffer[(Int, Int)]()
    l2: scala.collection.mutable.ListBuffer[(Int, Int)] = ListBuffer()
    
    scala> l2 += (1, 2)
    <console>:9: error: type mismatch;
     found   : Int(1)
     required: (Int, Int)
                  l2 += (1, 2)
    

.. The compiler thinks you are trying to add multiple Ints to the ListBuffer, when you are trying to add a tuple. You need an extra set of parentheses.

..当您尝试添加元组时,编译器认为您正在尝试向 中添加多个Ints ListBuffer。您需要一组额外的括号。

 l1.zipWithIndex.slice(0,l1.length-1).foreach(x=> l2 += ((x._1,l1(x._2+1)) ))
  1. You can use sliding, which will create a "sliding window" across the collection to return a list of lists of a specific group size, with a step size of one by default:

    scala> List(0, 3, 6, 12, 14, 15, 16, 17).sliding(2)
               .map { case List(a, b) => (a, b) }.toList
    res10: List[(Int, Int)] = List((0,3), (3,6), (6,12), (12,14), (14,15), (15,16), (16,17))
    
  1. 您可以使用sliding,它将在集合中创建一个“滑动窗口”以返回特定组大小的列表列表,默认情况下步长为1:

    scala> List(0, 3, 6, 12, 14, 15, 16, 17).sliding(2)
               .map { case List(a, b) => (a, b) }.toList
    res10: List[(Int, Int)] = List((0,3), (3,6), (6,12), (12,14), (14,15), (15,16), (16,17))
    

回答by user1484819

besides the sliding, you could slide like following:

除了滑动,你还可以像下面这样滑动:

  val l1= List(0, 3, 6, 12, 14, 15, 16, 17)
  val l2 = l1.take(l1.size - 1).zip(l1.tail)

updated

更新

   l1.zip(l1.tail) works.