📜  python中的issubset

📅  最后修改于: 2020-07-19 02:46:44             🧑  作者: Mango

如果集合A的所有元素都存在于另一个作为参数传递的集合B中,则issubset()方法将返回True;如果不存在所有元素,则返回false。

句法:A.issubset(B)
检查A是否是B的子集。

返回值:
如果A是B的子集,则返回true,否则返回false。


代码1:

# Python program to demonstrate working of 
# issubset(). 
  
A = {4, 1, 3, 5} 
B = {6, 0, 4, 1, 5, 0, 3, 5} 
  
# Returns True 
print(A.issubset(B)) 
  
# Returns False 
# B is not subset of A 
print(B.issubset(A)) 

输出:

True
False

代码2:

# Another Python program to demonstrate working  
# of issubset(). 
A = {1, 2, 3} 
B = {1, 2, 3, 4, 5} 
C = {1, 2, 4, 5} 
  
# Returns True 
print(A.issubset(B)) 
  
# Returns False 
# B is not subset of A 
print(B.issubset(A)) 
  
# Returns False 
print(A.issubset(C)) 
  
# Returns True 
print(C.issubset(B))

输出: 

True
False
False
True