两个带有相同对象的列表根据“计数”排除对象。

发布于 2025-02-05 06:45:54 字数 556 浏览 2 评论 0原文

我有一些问题找到解决问题的解决方案。

我有2个列表,它们具有相同的对象,但没有相同数量的对象。因此,举一个快速示例:

List1: Object1(ID (1), Name (James)); Object2(ID(1), Name(John)); Object3(ID (1); Name(Jane)); Object4(ID (2), Name(Carl))

List2: Object1(ID(1), Name (James)), Object2(ID(1), Name(John)), Object3(ID (2), Name(Carl))

因此,在上面的示例中,我们需要删除任何具有ID(1)的对象,而我们的列表仅包含对象(4),因为我们没有相同数量的对象ID编号,如果不清楚,它并不总是我ID(1),它可以包含ID(7),ID(15),ID(123)同样。 这是一个非常简单的示例,实际上,列表将包含数百个对象,但前提是相同的。

(我们必须从列表1中删除比列表2中的任何对象1的任何对象1)

道歉,如果这令人困惑或解释不当,我会很乐意在需要时尝试详细说明。

谢谢

I have a bit of an issue finding a solution for a problem.

I have 2 lists, they have identical objects, but not the same amount of objects. So to give a quick example:

List1: Object1(ID (1), Name (James)); Object2(ID(1), Name(John)); Object3(ID (1); Name(Jane)); Object4(ID (2), Name(Carl))

List2: Object1(ID(1), Name (James)), Object2(ID(1), Name(John)), Object3(ID (2), Name(Carl))

So in above example we need to remove any object that has ID(1), leaving us with a list that only contains object(4), because we do not have the same amount of objects with the same ID number, and if this is not clear, it will not always me ID(1), it could contain ID(7), ID(15), ID(123) aswell.
This is a very simplified example, in reality the lists will contain several hundred objects, but the premise is the same.

(We have to remove any object from List 1 that counts more objects with an ID number than what exists in List 2)

Apologies if this is confusing or poorly explained, I will happily try to elaborate if needed.

Thanks

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

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

发布评论

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

评论(1

记忆で 2025-02-12 06:45:54

它可能不是最佳的,但

        static List<Person> Remove(List<Person> list1, List<Person> list2)
        {
            Dictionary<int, int> list1_count = new Dictionary<int, int>();
            Dictionary<int, int> list2_count = new Dictionary<int, int>();
            List<int> Ids_to_remove = new List<int>();

            foreach (var item in list1)
            {
                if (list1_count.ContainsKey(item.ID))
                {
                    list1_count[item.ID] = list1_count[item.ID] + 1;
                }
                else
                {
                    list1_count.Add(item.ID, 1);
                }
            }

            foreach (var item in list2)
            {
                if (list2_count.ContainsKey(item.ID))
                {
                    list2_count[item.ID] = list2_count[item.ID] + 1;
                }
                else
                {
                    list2_count.Add(item.ID, 1);
                }
            }

            foreach (var item in list1_count)
            {
                if(list2_count.ContainsKey(item.Key))
                {
                    if(item.Value > list2_count[item.Key])
                    {
                        Ids_to_remove.Add(item.Key);
                    }
                }
            }

            foreach (var item in Ids_to_remove)
            {
                
                list1.RemoveAll(X => X.ID == item);
            }

            return list1;
        }

It might be not optimal but,

        static List<Person> Remove(List<Person> list1, List<Person> list2)
        {
            Dictionary<int, int> list1_count = new Dictionary<int, int>();
            Dictionary<int, int> list2_count = new Dictionary<int, int>();
            List<int> Ids_to_remove = new List<int>();

            foreach (var item in list1)
            {
                if (list1_count.ContainsKey(item.ID))
                {
                    list1_count[item.ID] = list1_count[item.ID] + 1;
                }
                else
                {
                    list1_count.Add(item.ID, 1);
                }
            }

            foreach (var item in list2)
            {
                if (list2_count.ContainsKey(item.ID))
                {
                    list2_count[item.ID] = list2_count[item.ID] + 1;
                }
                else
                {
                    list2_count.Add(item.ID, 1);
                }
            }

            foreach (var item in list1_count)
            {
                if(list2_count.ContainsKey(item.Key))
                {
                    if(item.Value > list2_count[item.Key])
                    {
                        Ids_to_remove.Add(item.Key);
                    }
                }
            }

            foreach (var item in Ids_to_remove)
            {
                
                list1.RemoveAll(X => X.ID == item);
            }

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