猿问

查找实例在列表中出现的次数

我有一个列表,我的目标是确定该列表中的值超过特定值的次数。


例如,如果我的列表是: List = {0, 0, 3, 3, 4, 0, 4, 4, 4} 我想知道有两个实例,我在列表中的值大于 2 并保持在上面2. 所以在这种情况下有 2 个实例,因为它一度低于 2 并再次高于它。


    private void Report_GeneratorButton_Click(object sender, EventArgs e)

    {

        //Lists

        var current = _CanDataGraph._DataPoints[CanDataGraph.CurveTag.Current].ToList();

        var SOC = _CanDataGraph._DataPoints[CanDataGraph.CurveTag.Soc].ToList();

        var highcell = _CanDataGraph._DataPoints[CanDataGraph.CurveTag.HighestCell].ToList();

        var lowcell = _CanDataGraph._DataPoints[CanDataGraph.CurveTag.LowestCell].ToList();


        //Seperates current list into charging, discharging, and idle

        List<double> charging = current.FindAll(i => i > 2);

        List<double> discharging = current.FindAll(i => i < -2);

        List<double> idle = current.FindAll(i => i < 2 && i > -2);


        //High cell

        List<double> overcharged = highcell.FindAll(i => i > 3.65);

        int ov = overcharged.Count;


        if (ov > 1)

        {

            Console.WriteLine("This Battery has gone over Voltage!");

        }


        else

        {

            Console.WriteLine("This battery has never been over Voltage.");

        }


        //Low cell

        List<double> overdischarged = lowcell.FindAll(i => i > 3.65);

        int lv = overdischarged.Count;


        if (lv > 1)

        {

            Console.WriteLine("This Battery has been overdischarged!");

        }


        else

        {

            Console.WriteLine("This battery has never been overdischarged.");

        }



        //Each value is 1 second

        int chargetime = charging.Count;

        int dischargetime = discharging.Count;

        int idletime = idle.Count;



        Console.WriteLine("Charge time: " + chargetime + "s" + "\n" + "Discharge time: " + dischargetime + "s" + "\n" + "Idle time: " + idletime);


    }

我当前的代码是这样并输出:


    This battery has never been over Voltage.

    This battery has never been overdischarged.

    Charge time: 271s

    Discharge time: 0s

    Idle time: 68


繁华开满天机
浏览 158回答 4
4回答

蛊毒传说

有很多方法可以解决这个问题;我的建议是将它分解成许多较小的问题,然后编写一个简单的方法来解决每个问题。这是一个更简单的问题:给定一个序列T,给我返回一个删除了“双倍”项目的序列T:public static IEnumerable<T> RemoveDoubles<T>(&nbsp; this IEnumerable<T> items)&nbsp;{&nbsp; T previous = default(T);&nbsp; bool first = true;&nbsp; foreach(T item in items)&nbsp; {&nbsp; &nbsp; if (first || !item.Equals(previous)) yield return item;&nbsp; &nbsp; previous = item;&nbsp; &nbsp; first = false;&nbsp; }}伟大的。这有什么帮助?因为现在解决您的问题是:int count = myList.Select(x => x > 2).RemoveDoubles().Count(x => x);跟着。如果您有myListas{0, 0, 3, 3, 4, 0, 4, 4, 4}那么结果Select是{false, false, true, true, true, false, true, true, true}.的结果RemoveDoubles是{false, true, false, true}。的结果Count是 2,这是期望的结果。尽可能使用现成的部件。如果你不能,试着解决一个简单的、一般的问题,让你得到你需要的东西;现在您有了一个工具,您可以将其用于需要您删除序列中重复项的其他任务。

蓝山帝景

该解决方案应该可以达到预期的效果。&nbsp; &nbsp; List<int> lsNums = new List<int>() {0, 0, 3, 3, 4, 0, 4, 4, 4} ;&nbsp; &nbsp; public void MainFoo(){&nbsp; &nbsp; &nbsp; &nbsp; int iChange = GetCritcalChangeNum(lsNums, 2);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Critical change = %d", iChange);&nbsp; &nbsp; }&nbsp; &nbsp; public int GetCritcalChangeNum(List<int> lisNum, int iCriticalThreshold) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; int iCriticalChange = 0;&nbsp; &nbsp; &nbsp; &nbsp; int iPrev = 0;&nbsp; &nbsp; &nbsp; &nbsp; lisNum.ForEach( (int ele) => {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(iPrev <= iCriticalThreshold && ele > iCriticalThreshold){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iCriticalChange++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iPrev = ele;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; return iCriticalChange;&nbsp; &nbsp; }

30秒到达战场

您可以创建一个扩展方法,如下所示。public static class ListExtensions{&nbsp; &nbsp; public static int InstanceCount(this List<double> list, Predicate<double> predicate)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int instanceCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; bool instanceOccurring = false;&nbsp; &nbsp; &nbsp; &nbsp; foreach (var item in list)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (predicate(item))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!instanceOccurring)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; instanceCount++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; instanceOccurring = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; instanceOccurring = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return instanceCount;&nbsp; &nbsp; }}并像这样使用您新创建的方法current.InstanceCount(p => p > 2)

手掌心

执行此操作的另一种方法System.Linq是遍历列表,同时选择itemitself 和 it's index,然后返回true每一项大于value且前一项小于或等于 的项value,然后选择结果数true。当然索引有一个特例0,我们不检查前一项:public static int GetSpikeCount(List<int> items, int threshold){&nbsp; &nbsp; return items?&nbsp; &nbsp; &nbsp; &nbsp; .Select((item, index) =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index == 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? item > threshold&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : item > threshold && items[index - 1] <= threshold)&nbsp; &nbsp; &nbsp; &nbsp; .Count(x => x == true)&nbsp; // '== true' is here for readability, but it's not necessary&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;?? 0;&nbsp; // return '0' if 'items' is null}&nbsp; &nbsp;示例用法:private static void Main(){&nbsp; &nbsp; var myList = new List<int> {0, 0, 3, 3, 4, 0, 4, 4, 4};&nbsp; &nbsp; var count = GetSpikeCount(myList, 2);&nbsp; &nbsp; // count == 2}
随时随地看视频慕课网APP
我要回答