如何在Mulitiple列表之间找到不同的项目
我正在尝试弄清楚如何比较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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Python的集合具有相交和差异属性。
使用此功能,您可以做您正在尝试的事情!
我有点担心这是一个家庭作业问题,所以我认为在这里完全详细说明是没有意义的。
Python has sets which have an intersection and a difference property.
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.
这是解决我问题的解决方案。
Here is a solution to my problem.
如问题所述,使用了一种设定的理论方法。由于未指定,因此未订购的最终标准师未订购。
As mentioned in the question a set-theoretical approach is used. Final sublists not ordered since not specified.