在 Python 中检查一个元组是否为另一个子集


如果需要检查一个元组是否是另一个元组的子集,则使用 'issubset' 方法。

'issubset' 方法将在所有元素都出现在另一个集合中时返回 True,其中另一个集合将作为参数传递给该方法。

否则,此方法将返回 False。

以下是相同的演示 -

示例

在线演示

my_tuple_1 = (87, 90, 31, 85)
my_tuple_2 = (34, 56, 12, 5)

print("The first tuple is :")
print(my_tuple_1)
print("The second tuple is :")
print(my_tuple_2)

my_result = set(my_tuple_2).issubset(my_tuple_1)

print("Is the second tuple a subset of the first tuple ? ")
print(my_result)

输出

The first tuple is :
(87, 90, 31, 85)
The second tuple is :
(34, 56, 12, 5)
Is the second tuple a subset of the first tuple ?
False

解释

  • 定义两个元组,并显示在控制台上。
  • 将第一个元组传递给 issubset 方法并将其与第二个元组进行比较。
  • 将此结果分配给一个值。
  • 它作为输出显示在控制台上。

更新于: 11-Mar-2021

2K+ 浏览量

开启你的 职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.