代码之家  ›  专栏  ›  技术社区  ›  User

C:从字典中删除重复值?

  •  18
  • User  · 技术社区  · 15 年前

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

    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()操作的内容?

    8 回复  |  直到 9 年前
        1
  •  46
  •   Jon Skeet    15 年前

    你想如何处理这些副本?如果您不介意丢失哪个键,只需创建另一个这样的字典:

    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);
    
        2
  •  7
  •   Daniel Brückner    15 年前

    蛮力解决方案如下

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

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

        3
  •  3
  •   Timothy Carter    15 年前

    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
  •  1
  •   queen3    15 年前
    foreach (var key in mydict.Keys)
      tempdict[mydict[key]] = key;
    foreach (var value in tempdict.Keys)
      uniquedict[tempdict[value]] = value;
    
        5
  •  1
  •   Sivvy    15 年前
    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
  •  1
  •   MILAD    11 年前

    我就是这样做的:

                    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
  •  0
  •   Guillaume V    13 年前

    除了jon skeet的答案之外,如果您的值是实习生对象,则可以使用:

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

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

        8
  •  0
  •   frank jock halliday    9 年前

    只需对那些使用revit API的人做一个脚注,这是一种在删除重复元素时有效的方法,当您不能使用say 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);
                         }
                     }