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

标签 c# 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

最佳答案

有很多方法可以解决这个问题;我的建议是将它分解成许多较小的问题,然后编写一个简单的方法来解决每个问题。

这是一个更简单的问题:给定一个 T 序列,返回一个删除了“双倍”项目的 T 序列:

public static IEnumerable<T> RemoveDoubles<T>(
  this IEnumerable<T> items) 
{
  T previous = default(T);
  bool first = true;
  foreach(T item in items)
  {
    if (first || !item.Equals(previous)) yield return item;
    previous = item;
    first = false;
  }
}

太棒了。这有什么帮助?因为现在您的问题的解决方案是:

int count = myList.Select(x => x > 2).RemoveDoubles().Count(x => x);

跟随。

如果您将 myList 作为 {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,这是想要的结果。

尽可能使用现成的部件。如果做不到,请尝试解决一个简单、一般的问题,从而满足您的需求;现在您有了一个工具,可用于需要您删除序列中重复项的其他任务。

关于c# - 查找实例在列表中出现的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56796892/

相关文章:

python - 将列表插入特定长度?

python - 中序二叉树遍历(使用Python)

python - 命名元组的字段中可以包含哪些数据类型?

c# - ASP.NET MVC Identity - 一起使用内部用户和 Azure AD 身份验证

c++ - 字符串的排列--error : no matching function for call to ‘std::set<char>::insert(std::string&)’

c# - TabItem/TabControl 中的 DataGridTextColumn 标题绑定(bind)问题

c# - asp.net-mvc: js 文件中的 razor '@' 符号

不完全由数字组成的单词的 Python 列表理解

c# - 如何增加长时间运行的查询的 executionTimeout?

c# - 如何从客户端机器获取 CultureInfo?