Python: See if one set contains another entirely?

前端 未结 7 1684
长情又很酷
长情又很酷 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 05:51

    If you suspect a set to be a subset of another, and intersect those two sets together, the result is equal to itself if it is a subset.

    a = [2,1,3,3]
    b = [5,4,3,2,1]
    set(a).intersection(set(b)) == set(a)
    >>True
    

提交回复
热议问题