【问题标题】:Longest list in SortedList of ListsSortedList of Lists 中最长的列表
【发布时间】:2014-02-03 15:28:17
【问题描述】:

我有一个 SortedList 列表,我有兴趣找到对应于最长列表(其中包含最多项目的列表)的 KEY。在代码中,它看起来像:

// how the list is defined:
var myList = new SortedList<long, List<string>>();

// EXAMPLE data only:
myList.Add(0, new List<string>());
myList[0].AddRange(new []{"a", "b", "c"});

myList.Add(8, new List<string>());
myList[8].AddRange(new []{"1", "2"});

myList.Add(23, new List<string>());
myList[23].AddRange(new []{"c", "d", "e", "f", "g"});

在上面的示例中,结果应该是“23”,因为这是最长列表的键。

我知道如何用 for 循环来写这个,但我认为这应该是用 LINQ 做的简单。也就是说,我似乎无法完全正确地使用语法!任何帮助表示赞赏!

【问题讨论】:

    标签: c# linq sortedlist


    【解决方案1】:

    也许有一种更有效的方法,但您可以按(价值)降序排列,然后先取。

    myList.OrderByDescending(m => m.Value.Count()).First().Key;
    

    当然,如果您希望所有键的计数最高(它们可能是具有相同长度的多个值),您应该按计数进行分组。

    类似的东西。

    myList.GroupBy(m => m.Value.Count())
          .OrderByDescending(m => m.Key)//I'm the key of the group by
          .First()
          .Select(g => g.Key);//I'm the key of the SortedList
    

    因此,如果您向示例中添加具有相同列表长度的项目

    myList.Add(24, new List<string>());
    myList[24].AddRange(new[] {"a", "b", "c", "d", "e"});
    

    你会得到 23 和 24。

    同样可以实现

    from item in myList
    let maxCount = myList.Max(x => x.Value.Count())
    where item.Value.Count() == maxCount
    select item.Key;
    

    【讨论】:

    • 其他一些选项可能会稍微快一些,但我正在处理小列表,这很容易阅读,所以它很完美!谢谢!
    【解决方案2】:

    虽然排序会给你正确的结果,但它需要 O(n log n) 时间来执行,同时比简单的 O(n) 扫描要渐近:

    int maxLength = myList.Max(x => x.Value.Count);
    var longestKeys = myList.Where(x => x.Value.Count == maxLength).Select(x => x.Key);
    

    【讨论】:

    • 这比使用OrderBy() 更好,因为它是 O(n),而排序需要 O(n log n)。如果你只想要一个结果,你也可以使用First()(或者Single())而不是Where()
    • @svick:正是 :-) 当你发表评论时,我在回答中写了这个。
    • @svick:如果 OP 只关心一个结果,那么使用 FirstSingle 是对的,但我想考虑到 OP 可能期望多个结果的情况。跨度>
    【解决方案3】:

    使用morelinq的MaxBy

    var key = myList.MaxBy(x => x.Value.Count()).Key;
    

    【讨论】:

      【解决方案4】:

      只是为了添加另一种方法,您可以使用 Linq 的 Aggregate 方法来实现这一点,如下所示:

      //Extension method
      public static long MaxIndex(this SortedList<long, List<string>> list)
      {
          return list.Aggregate(
              new { MaxValue = -1, Key = -1L },
                  ((agg, current) => (current.Value.Count.CompareTo(agg.MaxValue) > 0 || agg.Key == -1) ?
              new { MaxValue = current.Value.Count, Key = current.Key } :
              new { MaxValue = agg.MaxValue, Key = agg.Key })).
              Key;
      }
      
      // how the list is defined:
      var myList = new SortedList<long, List<string>>();
      
      // EXAMPLE data only:
      myList.Add(0, new List<string>());
      myList[0].AddRange(new[] { "a", "b", "c" });
      
      myList.Add(8, new List<string>());
      myList[8].AddRange(new[] { "1", "2" });
      
      myList.Add(23, new List<string>());
      myList[23].AddRange(new[] { "c", "d", "e", "f", "g" });
      
      var idx = myList.MaxIndex();
      

      这是改编自这个 SO 答案:https://stackoverflow.com/a/15068695/172769

      干杯

      【讨论】:

        猜你喜欢
        • 2022-12-17
        • 1970-01-01
        • 1970-01-01
        • 2014-06-10
        • 2014-06-05
        • 1970-01-01
        • 2022-12-02
        • 1970-01-01
        • 2021-06-22
        相关资源
        最近更新 更多