简体   繁体   English

如何计算列表中的最高、最低和平均数?

[英]How to calculate the highest, lowest and average number from a List?

cpuSensorValues.Add(sensor.Value);
if (cpuSensorValues.Count == 300)
{
    for (int i = 0; i < cpuSensorValues.Count; i++)
    {    
        Logger.Write("The Current CPU Temperature Is ===> " + sensor.Value);
    }
    cpuSensorValues = new List<float?>();
}

I'm adding to the List<float?> cpuSensorValues every second a value.List<float?> cpuSensorValues都会向List<float?> cpuSensorValues添加一个值。 Then when it's getting to 300 values I want to calculate the highest number the lowest number and the average number from the List write each number to the logger and then to make a new instance to the List.然后当它达到 300 个值时,我想计算列表中的最高数字、最低数字和平均数字,将每个数字写入记录器,然后在列表中创建一个新实例。

The problem is how can I calculate these 3 numbers?问题是我如何计算这 3 个数字?

You could use Enumerable.Max() , Enumerable.Min() , and Enumerable.Average() :您可以使用Enumerable.Max()Enumerable.Min()Enumerable.Average()

var min = cpuSensorValues.Min();
var max = cpuSensorValues.Max();
var avg = cpuSensorValues.Average();

However, this will enumerate the list 3 times (once per call).但是,这将枚举列表 3 次(每次调用一次)。 In practice, that's likely not going to be a problem, but if it is, you could just handle this yourself:在实践中,这可能不会成为问题,但如果是,您可以自己处理:

// Returns min/max/average, ignoring null elements
Tuple<float,float,float> ComputeStats(IEnumerable<float?> values)
{
     float min = float.MaxValue;
     float max = float.MinValue;
     float total = 0.0;
     int count = 0;

     foreach(var value in values)
     {
        if (value.HasValue)
        {
            min = Math.Min(min, value.Value);
            max = Math.Max(max, value.Value);
            total += value.Value;
            ++count;
        }
     }

     return Tuple.Create(min, max, total / count);
}

Given that you're adding the values to the list yourself - you could even keep a running total + min and max as you go.鉴于您自己将值添加到列表中 - 您甚至可以随时保持运行总计 + 最小值和最大值。 This would completely avoid needing to enumerate (or even store) the values.这将完全避免需要枚举(甚至存储)值。 The code above could easily be modified to handle this without storing the values in a List<T> at all.可以很容易地修改上面的代码来处理这个问题,而无需将值存储在List<T>中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从范围中获得最低和最高数字 - How to get lowest and highest number from range 如何从列表中找到最低和最高时间? - How to find lowest and highest time from list? 如何计算数组中的“平均”值,“最高”值和“最低”值? C# - How to calculate the “average” value , “highest” value , and “Lowest” value in an array? C# 如何找到最高和最低的数字C# - How to find the highest and the lowest number C# 如何在给定数组中找到最高,第二高的数字,最低的第二低的数字 - How to find highest ,second highest number, Lowest Second Lowest number in given Array 如何使用内置函数显示列表中重复次数最少和最多的数字? - How do you display the number that has the lowest and highest amount of duplicates in a list w/o using built in functions? 在列表C#中查找下一个最高和最低的数字 - Find next highest and next lowest number in list c# 如何有效地从列表中获取最高和最低值 <double?> ,然后修改它们? - How to efficiently get highest & lowest values from a List<double?>, and then modify them? (C#)如何从随机数生成器中获取数字并在消息框中输出最高和最低数字? - (C#) How can I take numbers from my random number generator and output the highest and lowest number in a message box? 从最高到最低对排行榜进行排序 - Sorting a leaderboard from highest to lowest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM