python 示例_带有示例的Python Set issubset()方法
python 示例
设置issubset()方法 (Set issubset() Method)
issubset() method is used to check whether this set (set1) is the subset of the set2, the method called with set1 and set2 is supplied as an argument, this method returns “True” is all elements of set1 present in the set2, else the method returns “False”.
issubset()方法用于检查此set(set1)是否为set2的子集,用set1和set2调用的方法作为参数提供,此方法返回“ True”是set2中存在的set1的所有元素,否则该方法返回“ False”。
Syntax:
句法:
set1.issubset(set2)
Parameter(s):
参数:
set1 – It represents the set1 (this set).
set1 –代表set1(此set)。
set2 – It represents the set2 (another set to be compared).
set2 –代表set2(另一个要比较的集合)。
Return value:
返回值:
The return type of this method is
此方法的返回类型为
Example 1:
范例1:
# Python Set issubset() Method with Example
# declaring the sets
cars_1 = {
"Porsche", "Audi", "Lexus"}
cars_2 = {
"Porsche", "Mazda", "Lincoln"}
cars_3 = {
"Porsche", "Audi", "Lexus", "Mazda", "Lincoln"}
# issubset() method call
result = cars_1.issubset(cars_2)
print("cars_1.issubset(cars_2): ", result)
result = cars_2.issubset(cars_3)
print("cars_2.issubset(cars_3): ", result)
result = cars_1.issubset(cars_3)
print("cars_1.issubset(cars_3): ", result)
# checking using condition
if cars_1.issubset(cars_2):
print("cars_1 is subest of cars_2")
else:
print("cars_1 is not subest of cars_2")
if cars_2.issubset(cars_3):
print("cars_2 is subest of cars_3")
else:
print("cars_2 is not subest of cars_3")
if cars_1.issubset(cars_3):
print("cars_1 is subest of cars_3")
else:
print("cars_1 is not subest of cars_3")
Output
输出量
cars_1.issubset(cars_2): False
cars_2.issubset(cars_3): True
cars_1.issubset(cars_3): True
cars_1 is not subest of cars_2
cars_2 is subest of cars_3
cars_1 is subest of cars_3
Example 2:
范例2:
# Python Set issubset() Method with Example
# declaring the sets
x = {
"ABC", "PQR", "XYZ"}
y = {
"ABC", "PQR", "XYZ"}
z = {
"DEF", "MNO", "UVW"}
# issubset() method calls
result = x.issubset(y)
print("x.issubset(y): ", result)
result = y.issubset(z)
print("y.issubset(z): ", result)
result = z.issubset(x)
print("z.issubset(x): ", result)
result = x.issubset(z)
print("x.issubset(z): ", result)
result = y.issubset(x)
print("y.issubset(x): ", result)
Output
输出量
x.issubset(y): True
y.issubset(z): False
z.issubset(x): False
x.issubset(z): False
y.issubset(x): True
翻译自: https://www.includehelp.com/python/set-issubset-method-with-example.aspx
python 示例
还没有评论,来说两句吧...