访问列表中的下一个元素以在 Scala 中进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5726766/
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
Accessing the next element in list to compare in Scala
提问by Dan
I'm new to Scala and i was wondering how you can call the next element of the list because I am trying to compare the current element with the adjacent one. Given x as the current element, I tried similar to java, x+1 but that didnt work. Any help?
我是 Scala 的新手,我想知道如何调用列表的下一个元素,因为我正在尝试将当前元素与相邻元素进行比较。给定 x 作为当前元素,我尝试了类似于 java, x+1 的方法,但是没有用。有什么帮助吗?
for (x <- list; if (x == (next adj. element))) println("same")
回答by Landei
How about sliding?
滑动呢?
val list = List(1,2,3,4)
list.sliding(2).foreach(println)
//List(1, 2)
//List(2, 3)
//List(3, 4)
回答by Rex Kerr
The canonical ways to do this in a forloop would be:
在for循环中执行此操作的规范方法是:
scala> val xs = List(1,2,3,4,3,2)
xs: List[Int] = List(1, 2, 3, 4, 3, 2)
scala> for (List(left,right) <- xs.sliding(2) if (left < right)) println(left + " < " + right)
1 < 2
2 < 3
3 < 4
scala> for ((left,right) <- (xs zip xs.tail) if (left < right)) println(left + " < " + right)
1 < 2
2 < 3
3 < 4
(Incidentally, you're probably better off putting the if statement outside rather than inside the for comprehension in this example.)
(顺便说一句,在这个例子中,你最好把 if 语句放在 for comprehension 外面而不是里面。)
If you have indices instead of values, you just dereference them using the same pattern. Personally, I don't find this pattern very clear or useful. It's slow, has weird corner-cases with lists that aren't full, and it's hard to follow what's going on. Instead, I define
如果您有索引而不是值,您只需使用相同的模式取消引用它们。就个人而言,我认为这种模式不是很清楚或有用。它很慢,有一些奇怪的角落案例,列表未满,并且很难跟踪正在发生的事情。相反,我定义
class PairedIterable[A](it: Iterable[A]) {
def foreachpair(f: (A,A) => Unit) = {
val i = it.iterator
if (i.hasNext) {
var prev = i.next
while (!ans && i.hasNext) {
val x = i.next
f(prev,x)
prev = x
}
}
}
}
implicit def iterable_has_pairs[A](it: Iterable[A]) = new PairedIterable(it)
which can then be used like so:
然后可以像这样使用:
scala> xs.foreachpair((left, right) => if (left < right) println(left + " < " + right))
1 < 2
2 < 3
3 < 4
Variants "forallpair", "existspair", and "findpair" are particularly useful.
变体“forallpair”、“existspair”和“findpair”特别有用。
回答by Daniel C. Sobral
This would be better handled by recursing over the list, instead of iterating through the elements, since elements don't know anything about the list.
这可以通过递归遍历列表而不是遍历元素来更好地处理,因为元素对列表一无所知。
For example:
例如:
def recurse[T](list: List[T]): Unit = list match {
case List(x, y, _*) if x == y =>
println("same")
recurse(list.tail)
case Nil =>
case _ => recurse(list.tail)
}
回答by tonek
As an option you may use matchand recursion instead of for:
作为一个选项,您可以使用match和递归代替for:
object Test {
def main(args: Array[String]) {
val list = List(1, 5, 3)
loop(list)
}
def loop(list: List[Int]) {
list match {
case Nil => println("Empty list")
case x :: Nil => println("last " + x)
case x :: tail => {
println(x + " - " + tail.head)
loop(tail)
}
}
}
}
回答by Eastsun
scala> val xs = 1::3::5::4::Nil
xs: List[Int] = List(1, 3, 5, 4)
scala> (xs, xs.tail).zip.foreach(println)
(1,3)
(3,5)
(5,4)
scala>
回答by Kyr
As in Scala 2.11.7 the following are valid:
在 Scala 2.11.7 中,以下内容是有效的:
scala> val xs = List(1,2,3,4)
xs: List[Int] = List(1, 2, 3, 4)
1) Zip the tail
1) 拉上尾巴
scala> xs.zip(xs.tail)
res0: List[(Int, Int)] = List((1,2), (2,3), (3,4))
2) Slide the window
2)滑动窗口
scala> xs.sliding(2)
res1: Iterator[List[Int]] = non-empty iterator
回答by Chris Murphy
list.tail.head
列表.tail.head
gives the next element if you want to go through all the elements from the front of the list. This is because the head is the front-most element and tail is the rest of the list.
如果要遍历列表前面的所有元素,则给出下一个元素。这是因为头部是最前面的元素,尾部是列表的其余部分。
回答by user unknown
scala> val li = List (3, 4, 5)
li: List[Int] = List(3, 4, 5)
scala> li.tail.head
res74: Int = 4

