C# 中 assoc-diff 的名称

发布于 2024-12-18 00:53:24 字数 207 浏览 2 评论 0原文

你怎么称呼这个方法,(它在.net中可用吗?)

var list1 = new List<int>() { 1, 2, 2, 3, 4 };
var list2 = new List<int>() { 1, 2, 3};
var results = list1.diff(list2);

results:
{ 2, 4 }

What do you call this method, (is it available in .net?)

var list1 = new List<int>() { 1, 2, 2, 3, 4 };
var list2 = new List<int>() { 1, 2, 3};
var results = list1.diff(list2);

results:
{ 2, 4 }

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

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

发布评论

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

评论(4

相对绾红妆 2024-12-25 00:53:24

内置的最接近的东西是 Except LINQ 运算符。

产生两个序列的集合差。

尽管按照您的示例,它会导致:

{ 4 }

我不相信有与您想要的直接类似的东西。

The closest thing built in is the Except LINQ operator.

Produces the set difference of two sequences.

Though with your example it will result in:

{ 4 }

I don't believe there is a direct analogue to what you want.

揽清风入怀 2024-12-25 00:53:24

您实际上需要一个多集实现。尽管 BCL 中没有开箱即用的多重集,但有一些想法 此处链接问题中。

或者你实际上可以自己实现一个,并没有那么复杂:

class Multiset<K> // maybe implement IEnumerable?
{
    Dictionary<K, int> arities = new Dictionary<K, int>();
    ...
    Multiset<K> Except(Multiset<K> other)
    {
        foreach (var k in arities.keys)
        {
            int arity = arities[k];
            if (other.Contains(k))
                arity -= other.Arity(k);
            if (arity > 0)
                result.Add(k, arity);
        }
        return result;
    }
}

You actually need a multiset implementation. Although there is no multiset out of the box in BCL, there are some ideas here and in the linked question.

Or you can actually implement one by yourself, it's not so complicated:

class Multiset<K> // maybe implement IEnumerable?
{
    Dictionary<K, int> arities = new Dictionary<K, int>();
    ...
    Multiset<K> Except(Multiset<K> other)
    {
        foreach (var k in arities.keys)
        {
            int arity = arities[k];
            if (other.Contains(k))
                arity -= other.Arity(k);
            if (arity > 0)
                result.Add(k, arity);
        }
        return result;
    }
}
自在安然 2024-12-25 00:53:24

这正是返回你想要的,你可以在扩展方法中重构它:

var results = list1.GroupBy(p => p).Select(p => new { item = p.Key, count = p.Count() })
                .Concat(list2.GroupBy(p => p).Select(p => new { item = p.Key, count = -p.Count() }))
                .GroupBy(p => p.item).Select(p => new { item = p.Key, count = p.Sum(q => q.count) })
                .Where(p => p.count > 0)
                .SelectMany(p => Enumerable.Repeat(p.item, p.count));

This exactly return what you want, You can refactor it in a Extension Method:

var results = list1.GroupBy(p => p).Select(p => new { item = p.Key, count = p.Count() })
                .Concat(list2.GroupBy(p => p).Select(p => new { item = p.Key, count = -p.Count() }))
                .GroupBy(p => p.item).Select(p => new { item = p.Key, count = p.Sum(q => q.count) })
                .Where(p => p.count > 0)
                .SelectMany(p => Enumerable.Repeat(p.item, p.count));
梦明 2024-12-25 00:53:24

像这样:(有关 linq to msdn,请参阅 oded 的帖子)

int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; 
int[] numbersB = { 1, 3, 5, 7, 8 }; 

IEnumerable<int> aOnlyNumbers = numbersA.Except(numbersB); 

Like this: (see oded's post for a linq to msdn)

int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; 
int[] numbersB = { 1, 3, 5, 7, 8 }; 

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