Test if set is a subset, considering the number (multiplicity) of each element in the set

前端 未结 5 1909
谎友^
谎友^ 2020-12-19 14:32

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 a

5条回答
  •  温柔的废话
    2020-12-19 15:08

    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
    

提交回复
热议问题