我知道我可以测试 set1 是否是 set2 的子集:
{'a','b','c'} <= {'a','b','c','d','e'} # True
但以下也是正确的:
{'a','a','b','c'} <= {'a','b','c','d','e'} # True
我如何让它考虑集合中的元素出现的次数,以便:
{'a','b','c'} <= {'a','b','c','d','e'} # True
{'a','a','b','c'} <= {'a','b','c','d','e'} # False since 'a' is in set1 twice but set2 only once
{'a','a','b','c'} <= {'a','a','b','c','d','e'} # True because both sets have two 'a' elements
我知道我可以做类似的事情:
A, B, C = ['a','a','b','c'], ['a','b','c','d','e'], ['a','a','b','c','d','e']
all([A.count(i) == B.count(i) for i in A]) # False
all([A.count(i) == C.count(i) for i in A]) # True
但我想知道是否有更简洁的东西set(A).issubset(B,count=True)
或一种远离列表推导的方法。谢谢!