如何找到列表的模式值? [英] How to find the mode value of a list?
本文介绍了如何找到列表的模式值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在scala集合中是否有函数来查找列表中值的最大出现次数,
可以说我有一个列表
$ bb$ p$ $ p $
L =列表(A,B,B,E,B,E,B ,B,C,E,B)
输出:B。
我可以编写一个模块来计算这个值,但我希望应该有一个scala方法或者斯卡拉收集功能来做到这一点,已经。感谢!
解决方案
我不知道现成的方法来做到这一点,但这是我的方式:
l.groupBy(i => i).mapValues(_。size).maxBy(_._ 2)。 _1
哦,但请注意,这并不处理模式不唯一的情况!
Is there a function in scala collections to find the max number of occurrence of a value in a list,
Lets say I have a list
L = List("A","B","B","E","B","E","B","B","C","E","B")
output: "B".
I can write a module to calculate this, but I would expect there should be a scala "way" or scala collection function to do this, already. Thanks!
解决方案
I don't know of a ready-made way to do it, but this is how I would do it:
l.groupBy(i => i).mapValues(_.size).maxBy(_._2)._1
Oh, but note this doesn't handle the case where the mode is not unique!
这篇关于如何找到列表的模式值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文