【问题标题】:C#: Counting Multiples in a List [closed]C#:计算列表中的倍数
【发布时间】:2015-04-06 15:31:12
【问题描述】:

所以我遇到的问题是我想计算列表中枚举的倍数。比如说,我有一个“Hi”“Hi”“Hello”“Hi”和“Hello”的列表。

有没有一种方法可以搜索数组并找出有 3 个“Hi”而无需执行 List.Where x = "Hi">?

非常感谢。

【问题讨论】:

  • 那么“你好”呢?你也要测试吗?请发布您的 c# 代码。
  • 我想知道“Hello”是否也有倍数。根据我在网上找到的内容以及我对 c# 的基本知识,我只看到了 .Where 功能,但是我必须预先定义要在列表中找到的枚举或字符串。我想知道是否有办法让它只计算枚举或字符串本身并返回它们的倍数。
  • 所以你想要一个只包含多个枚举的结果列表??
  • 到目前为止,您自己尝试过什么?如果您可以发布您当前(非首选)的方式,那将非常有帮助

标签: c# list enums count


【解决方案1】:

您可以尝试使用 Linq 来计算频率,例如:

List<String> source = new List<string>() {
  "Hi", "Hi", "Hello", "Hi", "Hello"
};

Dictionary<String, int> freqs = source
  .GroupBy(item => item)
  .ToDictionary(item => item.Key,
                item => item.Count());

....
int freqHi = freqs["Hi"]; // 3

【讨论】:

    【解决方案2】:

    此 Linq 查询将为您提供具有重复项的列表

     var mutiples = l.GroupBy(e => e).Where(c=>c.Count()>1);
    

    您可以使用此代码迭代多个列表

     foreach (var pair in mutiples)
                Console.WriteLine(pair.Key + " " + pair.Count());
    

    【讨论】:

      【解决方案3】:

      给你:

              public List<string> GetDuplicates(List<string> list)
              {
                  var q = from s in list
                          group s by s into g
                          select new { str = g.Key, count = g.Count() };
      
                  return q.Where(str => str.count > 1).Select(str => str.str).ToList<string>();
              }
      

      【讨论】:

        【解决方案4】:
        string words = "Hi, Hi, Hello, Hi, Hello";
        var countList = words.Split(new[] { " " }, StringSplitOptions.None);
        int count = countList.Where(s => s.Contains("Hi")).Count();
        

        运行此代码时计数 = 3。

        【讨论】:

          【解决方案5】:

          您必须先将它们分组,然后执行选择操作。如果您要求计算它们,一个示例可以是:

          List<string> stringList = new List<string>{ "hi", "hi", "hi", "hello" };
          stringList.GroupBy(i => i).Select(i => new {i.Key, i.ToList().Count }).ToList();
          

          【讨论】:

            【解决方案6】:

            要计算列表中某个元素的出现次数,您可以简单地执行以下操作:

            List.Count(x => x == "Hi");
            

            编辑:如果您只是想要一种简单的方法来判断哪个元素出现 N 次,您可以使用嵌套查询来完成:

            List<string> greetings = new List<string> {"Hi", "Hi", "Hello", "Hello", "Hi"};
            
            List<string> greetingsThatOccurThreeTimes = greetings
                .Where(s1 => greetings.Count(s2 => s1 == s2) == 3).ToList();
            

            EDIT #2:您也可以使用扩展方法来清理它。

            通过声明:

            public static class ListExtensions
            {
                public static List<T> WithNOccurrences<T>(this List<T> source, int n)
                {
                    return source.Where(s1 => source.Count(s2 => s1.Equals(s2)) == n).ToList();
                } 
            }
            

            你可以在你的调用代码中做这样的事情:

            List<string> greetings = new List<string> {"Hi", "Hi", "Hello", "Hello", "Hi"};
            
            // This list will only contain "Hi" (but 3 times, though)
            List<string> greetingsThatOccurThreeTimes = greetings.WithNOccurrences(3);
            

            这应该更类似于您的“ThreeOfAKind == true”。

            如果您只想取回每个出现 N 次的项目之一,只需将 .Distinct() 添加到扩展方法中,如下所示:

            public static class ListExtensions
            {
                public static List<T> WithNOccurrences<T>(this List<T> source, int n)
                {
                    return source
                        .Where(s1 => source.Count(s2 => s1.Equals(s2)) == n)
                        .Distinct().ToList();
                } 
            }
            

            【讨论】:

            • 嗯,我在想的是列表中有 3 个 hi 和 2 个 hello。我们想计算这些项目的倍数。这些项目可能在 list.GroupBy 函数中工作,但是我想知道当我们遍历列表并在列表中找到三个“hi”时,是否有一种方法可以让“threeofAkind = true”。
            • 我已经添加了一个扩展方法来做这件事。这就是你要找的东西吗?
            • 这正是我想要的。谢谢你。我对编码比较陌生,可以说大学并没有完全教会我们这样的东西。
            • 我不认为你会想要大学教你这样的东西 - 从基本面的角度来看,这并不是那么重要。你一路上捡起这些小“额外的”。不要忘记接受您认为可以回答您的问题的答案 :)
            【解决方案7】:

            请参阅此 SO 帖子 https://stackoverflow.com/a/10812657/1339616

            您也可以像这样使用 LINQ 查询语法。

            from    s in new List<string> {"Hi", "Hi", "Hello", "Hi", "Hello"}
            group   s by s into g
            where   g.Count() > 1
            orderby g.Count() descending
            select  new {Key = g.Key, Count = g.Count() }
            

            【讨论】:

              猜你喜欢
              • 2020-10-31
              • 2019-11-08
              • 2011-05-12
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-03-10
              • 1970-01-01
              相关资源
              最近更新 更多