检查一个元组是否是Python中另一个元组的子集
当需要检查一个元组是否是另一个元组的子集时,使用'issubset'方法。
如果该集合的所有元素都存在于另一个集合中,则“issubset”方法将返回True,其中另一个集合将作为参数传递给该方法。
否则,此方法返回False。
以下是相同的演示-
示例
my_tuple_1 = (87, 90, 31, 85)
my_tuple_2 = (34, 56, 12, 5)
print("Thefirsttupleis:")
print(my_tuple_1)
print("Thesecondtupleis:")
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)输出结果Thefirsttupleis: (87, 90, 31, 85) Thesecondtupleis: (34, 56, 12, 5) Is the second tuple a subset of the first tuple ? False
解释
定义了两个元组,并将其显示在控制台上。
通过将第一个元组传递给issubset方法,并将其与第二个元组进行比较来使用issubset方法。
该结果分配给一个值。
它在控制台上显示为输出。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下