Python: See if one set contains another entirely?

前端 未结 7 1678
长情又很酷
长情又很酷 2020-12-13 05:11

Is there a fast way to check if one set entirely contains another?

Something like:

>>>[1, 2, 3].containsAll([2, 1])
True

>>>[1, 2,         


        
相关标签:
7条回答
  • 2020-12-13 06:15

    Those are lists, but if you really mean sets you can use the issubset method.

    >>> s = set([1,2,3])
    >>> t = set([1,2])
    >>> t.issubset(s)
    True
    >>> s.issuperset(t)
    True
    

    For a list, you will not be able to do better than checking each element.

    0 讨论(0)
提交回复
热议问题