C#:如何比较字典值?

发布于 2024-10-09 18:57:47 字数 1434 浏览 2 评论 0原文

我有一个 Dictionary 如下

 Dictionary<String, String> MyDict = new Dictionary<String, String>();

,其中包含

 MyDict.Add("A", "1010");
 MyDict.Add("B", "1011");
 MyDict.Add("C", "1110");
 MyDict.Add("D", "1010");
 MyDict.Add("E", "1011");
 MyDict.Add("F", "1010");

我需要比较字典Values,然后添加 key 这里所有具有相同值的

是我生成的字典

 Dictionary<String, List<String>> MyResultDict = new Dictionary<String, List<String>>();

和我的代码

 var XXX = MyDict.ToLookup(X => X.Value, X => X.Key);
 MyResultDict = MyDict.ToDictionary(X => X.Key, X => XXX[X.Value].ToList());

上面的代码将产生类似的结果

 { "A"     { "A" , "D", "F" } }
 { "B"     { "B" , "E" } }
 { "C"     { "C" } }
 { "D"     { "A" , "D", "F" } }
 { "E"     { "B" , "E" } }
 { "F"     { "A" , "D", "F" } }

但我的解决方案有两个问题

  1. 存在重复值列表(对于键DEF。)

  2. 值列表包含密钥。

预期输出就像

 { "A"     { "D", "F" } }
 { "B"     { "E" } }

不需要key C,因为C的值在任何地方都不会重复。

并且不需要包含键 D 、 E 和 F,因为它已经包含在 AB 的值列表中。

如何使用 LinqLambda 表达式 执行此操作?

I have a Dictionary<String,String> as follows

 Dictionary<String, String> MyDict = new Dictionary<String, String>();

and which contains

 MyDict.Add("A", "1010");
 MyDict.Add("B", "1011");
 MyDict.Add("C", "1110");
 MyDict.Add("D", "1010");
 MyDict.Add("E", "1011");
 MyDict.Add("F", "1010");

I need to Compare the Dictionary Values and then to add the keys which all having same values

here is My resulting Dictionary

 Dictionary<String, List<String>> MyResultDict = new Dictionary<String, List<String>>();

And My code

 var XXX = MyDict.ToLookup(X => X.Value, X => X.Key);
 MyResultDict = MyDict.ToDictionary(X => X.Key, X => XXX[X.Value].ToList());

The above code will produce the result like

 { "A"     { "A" , "D", "F" } }
 { "B"     { "B" , "E" } }
 { "C"     { "C" } }
 { "D"     { "A" , "D", "F" } }
 { "E"     { "B" , "E" } }
 { "F"     { "A" , "D", "F" } }

But my solution is having two problem

  1. There exist Duplicate value list ( for the keys D , E and F.)

  2. The Value List includes the key.

The Expected OutPut is like

 { "A"     { "D", "F" } }
 { "B"     { "E" } }

i.e There is no need of key C because C's value is not repeated anywhere.

and there is no need to include keys D , E and F because its already included in A's and B's value lists.

How to do this using Linq or Lambda Expression?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风筝有风,海豚有海 2024-10-16 18:57:47
Dictionary<string, List<string>> result = myDict
    .GroupBy(kvp => kvp.Value)
    .Select(grp => new { Key = grp.First().Key, Matches = grp.Skip(1).Select(k => k.Key).ToList() })
    .Where(m => m.Matches.Count > 0)
    .ToDictionary(m => m.Key, m => m.Matches);

或者可能更简单一点:

Dictionary<string, List<string>> result = myDict
    .GroupBy(kvp => kvp.Value)
    .Where(grp => grp.Count() > 1)
    .ToDictionary(grp => grp.First().Key, grp => grp.Skip(1).Select(k => k.Key).ToList());
Dictionary<string, List<string>> result = myDict
    .GroupBy(kvp => kvp.Value)
    .Select(grp => new { Key = grp.First().Key, Matches = grp.Skip(1).Select(k => k.Key).ToList() })
    .Where(m => m.Matches.Count > 0)
    .ToDictionary(m => m.Key, m => m.Matches);

or possibly a bit simpler:

Dictionary<string, List<string>> result = myDict
    .GroupBy(kvp => kvp.Value)
    .Where(grp => grp.Count() > 1)
    .ToDictionary(grp => grp.First().Key, grp => grp.Skip(1).Select(k => k.Key).ToList());
陪我终i 2024-10-16 18:57:47

这应该可以做到:

var MyResultDict =
    MyDict
    .GroupBy(e => e.Value)
    .Where(g => g.Count() > 1)
    .ToDictionary(
        g => g.First().Key,
        g => g.Select(e => e.Key).Skip(1).ToList());

这里的关键(如果您能原谅这个双关语)是 GroupBy 方法 - 它根据您的比较将枚举元素收集在一起。完成此操作后,删除单例并将剩余元素转换为新字典就很简单了。

This should do it:

var MyResultDict =
    MyDict
    .GroupBy(e => e.Value)
    .Where(g => g.Count() > 1)
    .ToDictionary(
        g => g.First().Key,
        g => g.Select(e => e.Key).Skip(1).ToList());

The key here (if you'll forgive the pun) is the GroupBy method - it collects together elements of an enumeration based on your comparison. Once you've done that, it's a simple case of removing the singletons and converting the remaining elements to a new Dictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文