【问题标题】:C#: Remove duplicate values from dictionary?C#:从字典中删除重复值?
【发布时间】:2010-11-30 12:45:23
【问题描述】:

如何从可能有重复值的字典中创建没有重复值的字典?

IDictionary<string, string> myDict = new Dictionary<string, string>();

myDict.Add("1", "blue");
myDict.Add("2", "blue");
myDict.Add("3", "red");
myDict.Add("4", "green");


uniqueValueDict = myDict.???

编辑:

-我不在乎保留哪个密钥。 - 有没有使用 Distinct() 操作的东西?

【问题讨论】:

标签: c# linq .net-3.5 dictionary


【解决方案1】:

您想对重复项做什么?如果您不介意丢失哪个键,只需像这样构建另一个字典:

IDictionary<string, string> myDict = new Dictionary<string, string>();

myDict.Add("1", "blue");
myDict.Add("2", "blue");
myDict.Add("3", "red");
myDict.Add("4", "green");

HashSet<string> knownValues = new HashSet<string>();
Dictionary<string, string> uniqueValues = new Dictionary<string, string>();

foreach (var pair in myDict)
{
    if (knownValues.Add(pair.Value))
    {
        uniqueValues.Add(pair.Key, pair.Value);
    }
}

这是假设您使用的是 .NET 3.5,诚然。如果您需要 .NET 2.0 解决方案,请告诉我。

这是一个基于 LINQ 的解决方案,我觉得它非常紧凑...

var uniqueValues = myDict.GroupBy(pair => pair.Value)
                         .Select(group => group.First())
                         .ToDictionary(pair => pair.Key, pair => pair.Value);

【讨论】:

  • 他不知道100K后循环回0,ruuhaha
  • 谢谢。 linq 解决方案是我一直在寻找的。好奇你能以某种方式使用 Distinct 扩展方法吗?
  • @User:我认为 Distinct 在这种情况下不会有帮助......但是来自 MoreLINQ 的 DistinctBy 会。
  • LINQ 方法并非在所有情况下都有效。某些字典集不允许您调用 .GroupBy() 方法。
  • @Mitchell:你能提供更多细节吗? “字典集”是什么意思?
【解决方案2】:

蛮力解决方案如下所示

var result = dictionary
    .GroupBy(kvp => kvp.Value)
    .ToDictionary(grp => grp.First().Value, grp.Key)

假设您并不真正关心用于表示一组重复项的键,并且可以接受重建字典。

【讨论】:

  • 我试图想象类似 linq 的解决方案,但没有 VS 触手可及。 +1 用于实现这种方法 ;-)
  • 没有编译,因为我在 First() 调用之后错过了 .Value,但修复了它。
  • 这对我不起作用。它创建了一个以“值”作为键和值的字典。
  • 这会生成一个字典,其中的值作为键和值! :-(
【解决方案3】:

Jon 在 .NET 3.5 解决方案方面击败了我,但如果您需要 .NET 2.0 解决方案,这应该可以工作:

        List<string> vals = new List<string>();
        Dictionary<string, string> newDict = new Dictionary<string, string>();
        foreach (KeyValuePair<string, string> item in myDict)
        {
            if (!vals.Contains(item.Value))
            {
                newDict.Add(item.Key, item.Value);
                vals.Add(item.Value);
            }
        }

【讨论】:

    【解决方案4】:
    foreach (var key in mydict.Keys)
      tempdict[mydict[key]] = key;
    foreach (var value in tempdict.Keys)
      uniquedict[tempdict[value]] = value;
    

    【讨论】:

      【解决方案5】:
      Dictionary<string, string> test = new Dictionary<string,string>();
      test.Add("1", "blue");
      test.Add("2", "blue");
      test.Add("3", "green");
      test.Add("4", "red");
      Dictionary<string, string> test2 = new Dictionary<string, string>();
      foreach (KeyValuePair<string, string> entry in test)
      {
          if (!test2.ContainsValue(entry.Value))
              test2.Add(entry.Key, entry.Value);
      }
      

      【讨论】:

        【解决方案6】:

        我就是这样做的:

                        dictionary.add(control, "string1");
                        dictionary.add(control, "string1");
                        dictionary.add(control, "string2");
                      int x = 0;
                for (int i = 0; i < dictionary.Count; i++)
                {         
                    if (dictionary.ElementAt(i).Value == valu)
                    {
                        x++;
                    }
                    if (x > 1)
                    {
                        dictionary.Remove(control);
                    }
                }
        

        【讨论】:

          【解决方案7】:

          除了 Jon Skeet 的回答,如果你的值是一个实习对象,你可以使用:

          var uniqueValues = myDict.GroupBy(pair => pair.Value.Property)
                               .Select(group => group.First())
                               .ToDictionary(pair => pair.Key, pair => pair.Value);
          

          这样您将只删除对象的一个​​属性上的重复项

          【讨论】:

            【解决方案8】:

            对于那些使用 Revit API 的人来说只是一个脚注,当您不能使用 wallType 作为对象类型而需要利用原始元素时,这是一种对我有用的方法来删除重复元素。这是一个美丽的伴侣。

              //Add Pair.value to known values HashSet
                             HashSet<string> knownValues = new HashSet<string>();
            
                            Dictionary<Wall, string> uniqueValues = new Dictionary<Wall, string>();
            
                             foreach (var pair in wall_Dict)
                             {
                                 if (knownValues.Add(pair.Value))
                                 {
                                     uniqueValues.Add(pair.Key, pair.Value);
                                 }
                             }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2019-09-26
              • 2018-04-14
              • 2012-02-03
              • 2017-09-04
              • 2019-09-08
              • 1970-01-01
              相关资源
              最近更新 更多