【问题标题】:.Net Get Most Common Values in Dictionary List.Net 获取字典列表中最常见的值
【发布时间】:2023-04-10 14:12:02
【问题描述】:

我一直在查看stackoverflow,但从未有人问过这种问题,所以我决定问它,我想出了解决方案,所以我不妨将它发布在答案部分。

假设您将 Key 值作为唯一的函数地址,而 Value 值只是一个可能常见但也可能是随机的值。您想要提取所有具有最大值的 Key 地址值常见的,也想单独获取最常见的值以进行进一步处理。

这是我解决它的方法。

        var MostCommonDictionaryValuesFirst = PersonalCraft_SubOffset1_FunctionAddress.GroupBy(_ => _.Value).OrderByDescending(_ => _.Count()).SelectMany(_ => _);

【问题讨论】:

  • 这是 Int 数组.. 我需要一个多元素数组字典
  • 那又怎样?它与您执行的操作完全相同 - 只要您可以对 dict 进行分组,您的 dict 具有什么样的值并不重要 - 无论是 int 还是类实例或诸如此类,要做的事情都是相同的。他的解决方案与您发布的解决方案完全相同:groupby、OrderBy、count ...

标签: .net linq dictionary lambda


【解决方案1】:
//Get most common value first
var GetMostCommonValue = PersonalCraft_SubOffset1_FunctionAddress.GroupBy(_ => _.Value).OrderByDescending(_ => _.Count()).SelectMany(_ => _).First().Value;

//Get a list of diction values that occur most common first.
var MostCommonDictionaryValuesFirst = PersonalCraft_SubOffset1_FunctionAddress.GroupBy(_ => _.Value).OrderByDescending(_ => _.Count()).SelectMany(_ => _);

//This gives you a nice list of all most common addresses.
var GetMostCommonAddresses = PersonalCraft_SubOffset1_FunctionAddress.Where(t => t.Value == GetMostCommonValue).ToList();
//This has a sorted list of most common first.. but still needs to remove non-most common from list.. so needs work
var GetMostCommonFirst = PersonalCraft_SubOffset1_FunctionAddress.GroupBy(_ => _.Value).OrderByDescending(_ => _.Count()).SelectMany(_ => _);

仍然不知道如何从字典列表中删除不常见的值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    相关资源
    最近更新 更多