在字典中删除重复 [英] Removing Duplicates in Dictionary

查看:106
本文介绍了在字典中删除重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一本字典,像这样,

 词典< INT,字符串> roadNames =新字典< INT,字符串>();

roadNames.Add(1,珀丽路);
roadNames.Add(2,Transmere路);
roadNames.Add(3,珀丽路);
roadNames.Add(4,珀丽路);
roadNames.Add(5,珀丽路);
roadNames.Add(6,珀丽路);
roadNames.Add(7,珀丽路);
roadNames.Add(8,布朗路);
roadNames.Add(9,哈罗德路);
 

有没有LINQ的解决方案,以消除那些彼此相邻的重复。我后的结果是含有这样的列表,

 珀丽路
Transmere路
珀丽路
布朗路
哈罗德路
 

请注意该珀丽Rd是仍在列表两次。这样做是为了删除重复是彼此相邻,在这种情况下,我们除去项目4,5,6,和7

1的项目是不是下一个项目3,因此它不会被删除。

更新:

不要担心字典没有被订购。对于一个列表,是为了解决方案就可以了。我能处理的顺序。即。

 名单,其中,串> roadNames =新的名单,其中,串>()
{
    珀丽路,
    Transmere路,
    // 等等
};
 

解决方案

假设你正在使用一个排序的字典,而不是(或任何其他有序结构),有两种选择。

利用无扩展

这是很简单的,如果你利用无扩展微软(的每个人应该):

  roadNames.Values​​ //如果列表中删除,而不是
         .ToObservable()
         .DistinctUntilChanged()
         .ToList();
 

您可以更改最后了ToList() ToEnumerable(),而不是如果你喜欢。

这将返回:

 珀丽路
Transmere路
珀丽路
布朗路
哈罗德路
 

使用的扩展方法

您可以使用 GroupAdjacent 扩展方法,例如:

  roadNames.Values​​ //如果列表中删除,而不是
         .GroupAdjacent((X,Y)=&X的催化剂== y)的
         。选择(X => x.First());
 

扩展方法:

 公共静态的IEnumerable< IEnumerable的< T>> GroupAdjacent< T>(
    这IEnumerable的< T>源,Func键< T,T,布尔>邻)
{
    变种G =新的名单,其中,T>();
    的foreach(VAR的X源)
    {
        如果(g.Count = 0&放大器;!&安培;!相邻(g.Last(),X))
        {
            得到的回报克;
            G =新的名单,其中,T>();
        }
        g.Add(X);
    }
    得到的回报克;
}
 

If I have a dictionary like so,

Dictionary<int, string> roadNames = new Dictionary<int, string>();

roadNames.Add(1, "Rosedale Rd");
roadNames.Add(2, "Transmere Rd");
roadNames.Add(3, "Rosedale Rd");
roadNames.Add(4, "Rosedale Rd");
roadNames.Add(5, "Rosedale Rd");
roadNames.Add(6, "Rosedale Rd");
roadNames.Add(7, "Rosedale Rd");
roadNames.Add(8, "Brown Rd");
roadNames.Add(9, "Harold Rd");

Is there a LINQ solution to remove the duplicates that are NEXT to each other. The result I am after is a list containing this,

Rosedale Rd
Transmere Rd
Rosedale Rd
Brown Rd
Harold Rd

Note that Rosedale Rd is still in the list twice. The idea is to remove duplicates that are next to each other, and in this case we are removing item 4, 5, 6, and 7.

Items 1 is not next to item 3, so it isn't removed.

UPDATE:

Don't worry about Dictionary not being ordered. Solutions for a list that is in order would be fine. I can handle the ordering. i.e.

List<string> roadNames = new List<string>()
{
    "Rosedale Rd",
    "Transmere Rd",
    // etc
};

解决方案

Assuming you're using a sorted dictionary instead (or any other sorted structure), there are two options.

Leverage Reactive Extensions

This is very simple if you leverage Reactive Extensions from Microsoft (which everyone should!):

roadNames.Values // remove if a list instead
         .ToObservable()
         .DistinctUntilChanged()
         .ToList();

You can change that final ToList() to to ToEnumerable() instead if you like.

This returns:

Rosedale Rd 
Transmere Rd 
Rosedale Rd 
Brown Rd 
Harold Rd 

Use an Extension Method

You can use a GroupAdjacent extension method as such:

roadNames.Values // remove if a list instead
         .GroupAdjacent((x,y) => x == y)
         .Select(x => x.First());

The extension method:

public static IEnumerable<IEnumerable<T>> GroupAdjacent<T>(
    this IEnumerable<T> source, Func<T, T, bool> adjacent)
{
    var g = new List<T>();
    foreach (var x in source)
    {
        if (g.Count != 0 && !adjacent(g.Last(), x))
        {
            yield return g;
            g = new List<T>();
        }
        g.Add(x);
    }
    yield return g;
}

这篇关于在字典中删除重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆