Python 元組的並集


在 Python 中,元組是一種由多個值組成的順序資料結構。它是一種不可變的資料結構。因此,獲取兩個元組的並集比較棘手。本文將探討一些最佳方法,並瞭解它們在 Python 中的實現。

在 Python 中,元組是不可變的,但它們可以包含可變物件。類似地,我們可以藉助可變資料結構來獲得我們想要的並集。

Python 元組的並集應用於去除重複項、合併資料來源、集合運算、資料去重、合併多個結果、資料庫操作、處理資料重疊以及機器學習中的集合運算。它在不同領域的多種資料操作和分析任務中提供了通用性和實用性。

方法 1:使用 set 和 ‘+’ 運算子

我們可以輕鬆地將元組轉換為集合。集合具有一個獨特的屬性,即不允許重複元素,因此可以透過簡單地新增兩個元組並透過 set() 函式傳遞來實現並集。

示例

#taking two tuples
tuple1 = (1,2,3,4)
tuple2 = (3,4,5,6,7)

#displaying the tuples
print("Tuple 1: "+ str(tuple1))
print("Tuple 2: "+ str(tuple2))

#union set
setUnionTuple = set(tuple1+tuple2)
tupleUnion = tuple(setUnionTuple)

#printing the union of tuples
print("Union of tuples: "+ str(tupleUnion))

輸出

Tuple 1: (1, 2, 3, 4)
Tuple 2: (3, 4, 5, 6, 7)
Union of tuples: (1, 2, 3, 4, 5, 6, 7)

使用加號運算子將兩個元組相加,生成一個包含兩個元組中所有元素的元組。然後將其轉換為集合,從而刪除所有重複元素。這樣,我們就得到了一個最終的集合,它是兩個元組的並集。

方法 2:使用 Set 的 union 方法

Set 的 union() 方法可以巧妙地用於我們的元組並集。我們將把各個元組轉換為集合,然後 union() 方法會給出並集。之後,可以將並集再次轉換為元組。

示例

def tupleUnion(tuple1,tuple2):
    set1 = set(tuple1)
    set2 = set(tuple2)
    unionSet = set1.union(set2)
    return tuple(unionSet)

tuple1 = (1,2,3,4)
tuple2 = (3,4,5,6,7)

#displaying the tuples
print("Tuple 1: "+ str(tuple1))
print("Tuple 2: "+ str(tuple2))

#union of tuples
tupleUni = tupleUnion(tuple1,tuple2)
print("Union of tuples: "+ str(tupleUni))

輸出

Tuple 1: (1, 2, 3, 4)
Tuple 2: (3, 4, 5, 6, 7)
Union of tuples: (1, 2, 3, 4, 5, 6, 7)

為了更好地實現和理解,我們建立了一個函式 tupleUnion()。該函式以兩個元組作為輸入。現在,各個元組被轉換為集合,並使用 union() 函式生成集合的並集。最後,為了得到所需的輸出,將並集轉換為元組。

方法 3:使用 * 運算子進行集合轉換

在 Python 中,我們使用 * 運算子來解包元組。我們可以解包元組並從中建立一個集合,並且集合會自動刪除重複元素。因此,我們以集合形式得到所需的並集,並且可以輕鬆地將其轉換回元組。讓我們看看程式碼邏輯

示例

def tupleUnion(tuple1,tuple2):
    unionSet = set((*tuple1,*tuple2))
    return tuple(unionSet)

#taking two tuples
tuple1 = (1,2,3,4)
tuple2 = (3,4,5,6,7)

#displaying the tuples
print("Tuple 1: "+ str(tuple1))
print("Tuple 2: "+ str(tuple2))

#union of tuples
tupleUni = tupleUnion(tuple1,tuple2)

#printing the union of tuples
print("Union of tuples: "+ str(tupleUni))

輸出

Tuple 1: (1, 2, 3, 4)
Tuple 2: (3, 4, 5, 6, 7)
Union of tuples: (1, 2, 3, 4, 5, 6, 7)

因此,您可以在此實現中看到,函式 tupleUnion() 接受兩個元組。使用 * 運算子的元組是元組的解包,用於建立我們的集合。轉換回元組並返回的集合是結果並集。

方法 4:利用 itertools 模組

有一個 itertools 模組,其中包含用於建立迭代器的函式。在這種最終方法中,我們將嘗試利用該模組的 chain() 方法來生成可以一起形成集合的迭代器。集合可以轉換回元組以獲得所需的並集。

示例

import itertools

def tupleUnion(tuple1,tuple2):
    chainIterables = itertools.chain(tuple1,tuple2)
    unionSet = set(chainIterables)
    return tuple(unionSet)

#taking two tuples
tuple1 = (1,2,3,4)
tuple2 = (3,4,5,6,7)

#displaying the tuples
print("Tuple 1: "+ str(tuple1))
print("Tuple 2: "+ str(tuple2))

#union of tuples
tupleUni = tupleUnion(tuple1,tuple2)

#printing the union of tuples
print("Union of tuples: "+ str(tupleUni))

輸出

Tuple 1: (1, 2, 3, 4)
Tuple 2: (3, 4, 5, 6, 7)
Union of tuples: (1, 2, 3, 4, 5, 6, 7)

首先,當然我們需要匯入 itertools 模組。然後,在我們的元組並集函式中,我們將使用 itertools 模組中的 chain() 函式來建立迭代器的鏈。此鏈用於形成並集。它被轉換為元組作為輸出。

結論

本文總結了找到元組並集的幾種方法,包括使用 set 和 ‘+’ 運算子;set 的 union 方法;使用 * 運算子進行集合轉換;利用 itertools 模組。

元組作為不可變資料結構,主要用於表示固定的資料集合,找到它們的並集有助於各種資料操作結果。

更新於: 2023年8月29日

562 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告