如何在Mulitiple列表之间找到不同的项目

发布于 2025-01-24 16:46:52 字数 823 浏览 4 评论 0原文

我正在尝试弄清楚如何比较n列表以查找不同的项目。

例如,考虑列表:


list_l = [[1,2,3,4,5,100], 
          [1,2,31,41,51], 
          [10,2,3,45,25],  
          [4,20,3,4,12,51,200]
         ]

list_l中的每个列表中的每个项目都是唯一的。例如,列表[1,2,3,4,5,100]没有重复的元素。列表[1,2,31,41,51]等...


find_differences(list_l)

预期输出

res = [[5, 100], [31, 41], [10, 45, 25], [20, 12, 200]]
[5, 100] # elements of this list appears only in list [1,2,3,4,5,100]
[31, 41] # elements of this list appears only in list [1,2,31,41,51]
[10, 45, 25] # same here
[20, 12, 200] # same here

也只有4个列表和很少的项目。但是,我的用例还有更多列表,每个列表包含数百个项目。

使用SET函数给出了列表输入列表之间的差异。但是,结果列表并不能区分最初属于哪个列表的哪个项目。

任何帮助将不胜感激!

I am trying to figure out how to compare n lists to find the different items.

For example, consider the list:


list_l = [[1,2,3,4,5,100], 
          [1,2,31,41,51], 
          [10,2,3,45,25],  
          [4,20,3,4,12,51,200]
         ]

Each item is unique for each list in list_l. e.g. the list [1,2,3,4,5,100] has no duplicate elements. The same goes for the list [1,2,31,41,51], etc...


find_differences(list_l)

Expected outputs

res = [[5, 100], [31, 41], [10, 45, 25], [20, 12, 200]]
[5, 100] # elements of this list appears only in list [1,2,3,4,5,100]
[31, 41] # elements of this list appears only in list [1,2,31,41,51]
[10, 45, 25] # same here
[20, 12, 200] # same here

This example only has 4 lists and few items. However, my use case has many more lists and each list contains hundreds of items.

Using the set function gives a list of differences between the input lists. The resulting list, however, does not make it possible to distinguish which item originally belongs to which list.

Any help would be highly appreciated!

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

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

发布评论

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

评论(3

活雷疯 2025-01-31 16:46:53

Python的集合具有相交和差异属性。

s1 = set([1,2,3,4,5,100])
s2 = set([1,2,31,41,51])
s1.difference(s2)

使用此功能,您可以做您正在尝试的事情!

我有点担心这是一个家庭作业问题,所以我认为在这里完全详细说明是没有意义的。

Python has sets which have an intersection and a difference property.

s1 = set([1,2,3,4,5,100])
s2 = set([1,2,31,41,51])
s1.difference(s2)

Using this you can do what you're attempting!

I am a little worried this is a homework problem, so I don't think it makes sense to fully elaborate here.

长安忆 2025-01-31 16:46:53

这是解决我问题的解决方案。

results = []
for init_list in list_l:
    temp = []
    for e in init_list:
        unique_item = True
        for each_list in list_l:
            # Check if e is not unique, and if init_list and each_list are not the same list    
            if e in each_list and init_list != each_list:
                unique_item = False
                break
                
        # concatenate all unique items of a list
        if unique_item:
            temp.append(e)

    results.append(temp)

Here is a solution to my problem.

results = []
for init_list in list_l:
    temp = []
    for e in init_list:
        unique_item = True
        for each_list in list_l:
            # Check if e is not unique, and if init_list and each_list are not the same list    
            if e in each_list and init_list != each_list:
                unique_item = False
                break
                
        # concatenate all unique items of a list
        if unique_item:
            temp.append(e)

    results.append(temp)
百变从容 2025-01-31 16:46:53

如问题所述,使用了一种设定的理论方法。由于未指定,因此未订购的最终标准师未订购。

list_l = # from above

n = set(range(len(list_l)))
list_new = []
for i in range(len(list_l)):
    unique_entries = set(list_l[i]).difference(sum(map(list_l.__getitem__, n.difference({i})), []))
    list_new.append(list(unique_entries))

print(list_new)

As mentioned in the question a set-theoretical approach is used. Final sublists not ordered since not specified.

list_l = # from above

n = set(range(len(list_l)))
list_new = []
for i in range(len(list_l)):
    unique_entries = set(list_l[i]).difference(sum(map(list_l.__getitem__, n.difference({i})), []))
    list_new.append(list(unique_entries))

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