在 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.