I know I can test if set1 is a subset of set2 with:
{'a','b','c'} <= {'a','b','c','d','e'} # True
But the following is also True:
{'a','a','b','c'} <= {'a','b','c','d','e'} # True
How do I have it consider the number of times an element in the set occurs so that:
{'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
I know I could do something like:
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
But I was wondering if there was something more succinct like set(A).issubset(B,count=True)
or a way to stay from list comprehensions. Thanks!
As stated in the comments, a possible solution using Counter
:
from collections import Counter
def issubset(X, Y):
return len(Counter(X)-Counter(Y)) == 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With