C#:从字典中删除重复值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1462101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: Remove duplicate values from dictionary?
提问by User
How can I create a dictionary with no duplicate values from a dictionary that may have duplicate values?
如何从可能具有重复值的字典中创建一个没有重复值的字典?
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.???
Edit:
编辑:
-I don't care which key is kept. - Is there something using Distinct() operation?
- 我不在乎保留哪把钥匙。- 是否有使用 Distinct() 操作的东西?
采纳答案by Jon Skeet
What do you want to do with the duplicates? If you don't mind which key you lose, just build another dictionary like this:
你想对重复项做什么?如果您不介意丢失哪个键,只需像这样构建另一个字典:
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);
}
}
That assumes you're using .NET 3.5, admittedly. Let me know if you need a .NET 2.0 solution.
诚然,这假定您使用的是 .NET 3.5。如果您需要 .NET 2.0 解决方案,请告诉我。
Here's a LINQ-based solution which I find pleasantly compact...
这是一个基于 LINQ 的解决方案,我觉得它非常紧凑......
var uniqueValues = myDict.GroupBy(pair => pair.Value)
.Select(group => group.First())
.ToDictionary(pair => pair.Key, pair => pair.Value);
回答by queen3
foreach (var key in mydict.Keys)
tempdict[mydict[key]] = key;
foreach (var value in tempdict.Keys)
uniquedict[tempdict[value]] = value;
回答by Daniel Brückner
The brute-force solution would be something like the following
蛮力解决方案将类似于以下内容
var result = dictionary
.GroupBy(kvp => kvp.Value)
.ToDictionary(grp => grp.First().Value, grp.Key)
assuming you don't really care about the key used to represent a group of duplicates and it is acceptable to rebuild the dictionary.
假设您并不真正关心用于表示一组重复项的键,并且重建字典是可以接受的。
回答by Sivvy
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);
}
回答by Timothy Carter
Jon beat me to the .NET 3.5 solution, but this should work if you need a .NET 2.0 solution:
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);
}
}
回答by Guillaume V
In addition to the answer of Jon Skeet , if your value is an intern object you can use :
除了 Jon Skeet 的答案,如果您的值是实习对象,您可以使用:
var uniqueValues = myDict.GroupBy(pair => pair.Value.Property)
.Select(group => group.First())
.ToDictionary(pair => pair.Key, pair => pair.Value);
This way you will remove the duplicate only on one property of the object
这样,您将仅删除对象的一个属性上的重复项
回答by MILAD
This is how I did it:
我是这样做的:
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);
}
}
回答by frank jock halliday
Just a footnote to those using the Revit API, this is one method that works for me in removing duplicate elements, when you can't use say wallType as your object type and instead need to leverage raw elements. it's a beaut mate.
只是对那些使用 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);
}
}