如何找到一个列表&LT模式;双&GT ;? [英] How do I find the mode of a list<double>?

查看:140
本文介绍了如何找到一个列表&LT模式;双&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表:

 列表<双>最后=新的List<双>(); 
final.Add(1);
final.Add(2);
final.Add(3);



我可以用什么样的方法来找到这个列表的模式?另外,如果有两种模式,该函数将返回两者中的较小。


解决方案

 诠释? modeValue = 
最终
.GroupBy(X =&X的催化剂)
.OrderByDescending(X => x.Count())。ThenBy(X => x.Key)
。选择(X =>(INT)x.Key)
.FirstOrDefault();



它所需要的是一些镇静LINQ操作。您也可以表达对查询表达式是一样的。



如果列表为空, modeValue


I have a list:

List<double> final=new List<double>();
final.Add(1);
final.Add(2);
final.Add(3);

What sort of method could I use to find the mode of this list? Also, if there are two modes, the function would return the smaller of the two.

解决方案

int? modeValue =
 final
 .GroupBy(x => x)
 .OrderByDescending(x => x.Count()).ThenBy(x => x.Key)
 .Select(x => (int?)x.Key)
 .FirstOrDefault();

All it takes are a few composed LINQ operations. You can also express the same with query expressions.

If the list is empty, modeValue will be null.

这篇关于如何找到一个列表&LT模式;双&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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