如何在 Scala 的列表中找到最大值的索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14011181/
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
How can I find the index of the maximum value in a List in Scala?
提问by Phil
For a Scala List[Int] I can call the method max to find the maximum element value.
对于 Scala List[Int],我可以调用 max 方法来查找最大元素值。
How can I find the index of the maximum element?
如何找到最大元素的索引?
This is what I am doing now:
这就是我现在正在做的:
val max = list.max
val index = list.indexOf(max)
回答by Travis Brown
One way to do this is to zip the list with its indices, find the resulting pair with the largest first element, and return the second element of that pair:
一种方法是将列表及其索引压缩,找到具有最大第一个元素的结果对,并返回该对的第二个元素:
scala> List(0, 43, 1, 34, 10).zipWithIndex.maxBy(_._1)._2
res0: Int = 1
This isn't the most efficient way to solve the problem, but it's idiomatic and clear.
这不是解决问题的最有效方法,但它是惯用的和清晰的。
回答by Yuichiroh
Since Seqis a function in Scala, the following code works:
由于Seq是 Scala 中的函数,因此以下代码有效:
list.indices.maxBy(list)
回答by Keyel
def maxIndex[ T <% Ordered[T] ] (list : List[T]) : Option[Int] = list match {
case Nil => None
case head::tail => Some(
tail.foldLeft((0, head, 0)){
case ((indexOfMaximum, maximum, index), elem) =>
if(elem > maximum) (index, elem, index + 1)
else (indexOfMaximum, maximum, index + 1)
}._1
)
} //> maxIndex: [T](list: List[T])(implicit evidence: T => Ordered[T])Option[Int]
maxIndex(Nil) //> res0: Option[Int] = None
maxIndex(List(1,2,3,4,3)) //> res1: Option[Int] = Some(3)
maxIndex(List("a","x","c","d","e")) //> res2: Option[Int] = Some(1)
maxIndex(Nil).getOrElse(-1) //> res3: Int = -1
maxIndex(List(1,2,3,4,3)).getOrElse(-1) //> res4: Int = 3
maxIndex(List(1,2,2,1)).getOrElse(-1) //> res5: Int = 1
In case there are multiple maximums, it returns the first one's index.
如果有多个最大值,则返回第一个的索引。
Pros:You can use this with multiple types, it goes through the list only once, you can supply a default index instead of getting exception for empty lists.
优点:您可以将它用于多种类型,它只遍历列表一次,您可以提供默认索引而不是为空列表获取异常。
Cons:Maybe you prefer exceptions :) Not a one-liner.
缺点:也许你更喜欢例外:) 不是单线。
回答by xhudik
even easier to read would be:
更容易阅读的是:
val g = List(0, 43, 1, 34, 10)
val g_index=g.indexOf(g.max)
回答by Plasty Grove
Pimp my library! :)
皮条客我的图书馆!:)
class AwesomeList(list: List[Int]) {
def getMaxIndex: Int = {
val max = list.max
list.indexOf(max)
}
}
implicit def makeAwesomeList(xs: List[Int]) = new AwesomeList(xs)
//> makeAwesomeList: (xs: List[Int])scalaconsole.scratchie1.AwesomeList
//Now we can do this:
List(4,2,7,1,5,6) getMaxIndex //> res0: Int = 2
//And also this:
val myList = List(4,2,7,1,5,6) //> myList : List[Int] = List(4, 2, 7, 1, 5, 6)
myList getMaxIndex //> res1: Int = 2
//Regular list methods also work
myList filter (_%2==0) //> res2: List[Int] = List(4, 2, 6)
More details about this pattern here: http://www.artima.com/weblogs/viewpost.jsp?thread=179766
有关此模式的更多详细信息,请访问:http: //www.artima.com/weblogs/viewpost.jsp?thread=179766

