如何验证一个列表是否是另一个列表的子集

如何验证一个列表是否是另一个列表的子集

本文介绍了如何验证一个列表是否是另一个列表的子集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证列表是否是另一个列表的子集-我想要的只是布尔返回值.

I need to verify if a list is a subset of another - a boolean return is all I seek.

在相交后测试较小列表上的相等性是最快的方法吗?鉴于需要比较的数据集数量,性能至关重要.

Is testing equality on the smaller list after an intersection the fastest way to do this? Performance is of utmost importance given the number of datasets that need to be compared.

根据讨论添加更多事实:

Adding further facts based on discussions:

  1. 在许多测试中,两个列表中的两个列表是否相同?它确实是其中之一,因为它是一个静态查找表.

  1. Will either of the lists be the same for many tests? It does as one of them is a static lookup table.

是否需要一个列表?事实并非如此-静态查找表可以是执行效果最好的任何内容.动态命令是一种字典,我们从中提取密钥以执行静态查找.

Does it need to be a list? It does not - the static lookup table can be anything that performs best. The dynamic one is a dict from which we extract the keys to perform a static lookup on.

在这种情况下,最佳解决方案是什么?

What would be the optimal solution given the scenario?

推荐答案

Python为此提供的性能函数是 set.issubset .但是,它确实有一些限制,使得不清楚它是否是您问题的答案.

The performant function Python provides for this is set.issubset. It does have a few restrictions that make it unclear if it's the answer to your question, however.

列表可能包含多个项目并具有特定顺序.一套没有.要获得高性能集,请仅在 hashable 对象上工作.

A list may contain items multiple times and has a specific order. A set does not. To achieve high performance sets work on hashable objects only.

您是否要查询子集或子序列(这意味着您需要一个字符串搜索算法)?在许多测试中,两个列表中的两个列表是否相同?列表中包含哪些数据类型?就此而言,它是否需要成为列表?

Are you asking about subset or subsequence (which means you'll want a string search algorithm)? Will either of the lists be the same for many tests? What are the datatypes contained in the list? And for that matter, does it need to be a list?

您的其他帖子与字典和列表相交使类型更清晰,确实获得了建议使用字典键视图来实现类似集合的功能.在那种情况下,之所以可以工作是因为字典键的行为就像一个集合(以至于在我们使用Python进行集合之前,我们都使用字典).一个人想知道问题如何在三个小时内变得不那么具体.

Your other post intersect a dict and list made the types clearer and did get a recommendation to use dictionary key views for their set-like funcitonality. In that case it was known to work because dictionary keys behave like a set (so much so that before we had sets in Python we used dictionaries). One wonders how the issue got less specific in three hours.

这篇关于如何验证一个列表是否是另一个列表的子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 06:01