我需要验证一个列表是否是另一个列表的子集-布尔返回是我所寻求的。

在交叉路口后的小列表上测试相等性是最快的方法吗?考虑到需要比较的数据集的数量,性能是极其重要的。

在讨论的基础上补充进一步的事实:

对于许多测试,这两个列表是否都是相同的?其中一个是静态查找表。 需要是一个列表吗?它不是——静态查找表可以是任何性能最好的表。动态的是一个字典,我们从中提取键来执行静态查找。

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


当前回答

在python 3.5中,您可以执行[*set()][index]来获取元素。它比其他方法要慢得多。

one = [1, 2, 3]
two = [9, 8, 5, 3, 2, 1]

result = set(x in two for x in one)

[*result][0] == True

或者只用len和set

len(set(a+b)) == len(set(a))

其他回答

在python 3.5中,您可以执行[*set()][index]来获取元素。它比其他方法要慢得多。

one = [1, 2, 3]
two = [9, 8, 5, 3, 2, 1]

result = set(x in two for x in one)

[*result][0] == True

或者只用len和set

len(set(a+b)) == len(set(a))

如果你在问一个列表是否“包含”在另一个列表中,那么:

>>>if listA in listB: return True

如果你想知道listA中的每个元素在listB中是否有相等数量的匹配元素,试试:

all(True if listA.count(item) <= listB.count(item) else False for item in listA)
>>> a = [1, 3, 5]
>>> b = [1, 3, 5, 8]
>>> c = [3, 5, 9]
>>> set(a) <= set(b)
True
>>> set(c) <= set(b)
False

>>> a = ['yes', 'no', 'hmm']
>>> b = ['yes', 'no', 'hmm', 'well']
>>> c = ['sorry', 'no', 'hmm']
>>> 
>>> set(a) <= set(b)
True
>>> set(c) <= set(b)
False

由于没有人考虑比较两个字符串,下面是我的建议。

当然,您可能想检查管道(“|”)是否不属于这两个列表,可能会自动选择另一个char,但您已经明白了。

使用空字符串作为分隔符不是一个解决方案,因为数字可以有几个数字([12,3]!= [1,23])

def issublist(l1,l2):
    return '|'.join([str(i) for i in l1]) in '|'.join([str(i) for i in l2])

下面的代码检查一个给定的集合是否是另一个集合的“适当子集”

 def is_proper_subset(set, superset):
     return all(x in superset for x in set) and len(set)<len(superset)