在 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 方法并将其与第二个元组进行比较。
- 将此结果分配给一个值。
- 它作为输出显示在控制台上。
广告
数据结构
网络
数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP