如何在Python中將一個元組新增到另一個元組中?


在本文中,我們將向您展示如何在Python中將一個元組新增到另一個元組中。以下是完成此任務的各種方法:

  • 使用 + 運算子。

  • 使用 sum() 函式。

  • 使用 list() 和 extend() 函式。

  • 使用解包(*)運算子。

元組是Python中用於儲存集合的不可變、無序資料型別。列表和元組在許多方面都很相似,但是列表的長度是可變的,並且是可變的,而元組的長度是固定的並且是**不可變的**。

使用 + 運算子

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入元組1。

  • 建立另一個變數來儲存輸入元組2。

  • 使用**+ 運算子**將第二個元組追加或連線到第一個元組。

  • 列印將inputTuple_2追加到inputTuple_1後的結果元組。

示例

下面的程式使用 + 運算子將inputTuple_2追加到inputTuple_1:

# input tuple 1 inputTuple_1 = (12, 8, 6) # input tuple 2 inputTuple_2 = (3, 4) # appending/concatenating 2nd tuple to the first tuple using the + operator resultTuple = inputTuple_1 + inputTuple_2 # printing the resultant tuple after appending print("Resultant tuple after appending inputTuple_2 to the inputTuple_1:\n", resultTuple)

輸出

執行上述程式後,將生成以下輸出:

Resultant tuple after appending inputTuple_2 to the inputTuple_1:
(12, 8, 6, 3, 4)

使用 sum() 函式

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入元組1。

  • 建立另一個變數來儲存輸入元組2。

  • 列印兩個給定的輸入元組。

  • 將兩個元組作為一個元組和一個空元組作為引數傳遞給**sum()**函式(返回可迭代物件中所有項的和)以連線這兩個給定的元組。

  • 列印將inputTuple_2追加到inputTuple_1後的結果元組。

示例

下面的程式使用 sum() 函式將inputTuple_2追加到inputTuple_1:

# input tuple 1 inputTuple_1 = (12, 8, 6) # input tuple 2 inputTuple_2 = (3, 4) # printing both the given input tuples print("inputTuple_1: ", inputTuple_1) print("inputTuple_2: ", inputTuple_2) # appending/concatenating 2nd tuple to the first tuple resultTuple = sum((inputTuple_1, inputTuple_2), ()) # printing the resultant tuple after appending print("Resultant tuple after appending inputTuple_2 to the inputTuple_1:\n", resultTuple)

輸出

執行上述程式後,將生成以下輸出:

inputTuple_1: (12, 8, 6)
inputTuple_2: (3, 4)
Resultant tuple after appending inputTuple_2 to the inputTuple_1:
(12, 8, 6, 3, 4)

使用 list() 和 extend() 函式

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入元組1。

  • 建立另一個變數來儲存輸入元組2。

  • 列印兩個給定的輸入元組。

  • 使用**list()**函式(將序列/可迭代物件轉換為列表)將兩個輸入元組轉換為列表。

  • 使用**extend()函式**(將可迭代物件(如列表、元組、字串等)的所有元素新增到列表的末尾)來追加或連線上面的第二個列表到第一個列表。

  • 使用**tuple()**函式(在Python中建立元組)將結果的第一個列表轉換為元組,這是將第二個列表追加到第一個列表後的結果元組。

  • 列印追加後的結果元組。

示例

下面的程式使用list和extend()函式將inputTuple_2追加到inputTuple_1:

# input tuple 1 inputTuple_1 = (12, 8, 6) # input tuple 2 inputTuple_2 = (3, 4) # printing both the given input tuples print("inputTuple_1: ", inputTuple_1) print("inputTuple_2: ", inputTuple_2) # converting inputTuple_1 into list list_1 = list(inputTuple_1) # converting inputTuple_2 into list list_2 = list(inputTuple_2) # appending/concatenating 2nd list to the first list list_1.extend(list_2) # converting the resultant first list to a tuple # which is the resultant tuple after appending the 2nd list to the 1st list resultTuple=tuple(list_1) # printing the resultant tuple after appending print("Resultant tuple after appending inputTuple_2 to the inputTuple_1:\n", resultTuple)

輸出

執行上述程式後,將生成以下輸出:

inputTuple_1: (12, 8, 6)
inputTuple_2: (3, 4)
Resultanat tuple after appending inputTuple_2 to the inputTuple_1:
(12, 8, 6, 3, 4)

使用解包(*)運算子

示例

下面的程式使用解包運算子將inputTuple_2追加到inputTuple_1:

# input tuple 1 inputTuple_1 = (12, 8, 6) # input tuple 2 inputTuple_2 = (3, 4) # printing both the given input tuples print("inputTuple_1: ", inputTuple_1) print("inputTuple_2: ", inputTuple_2) # Unpacking the first tuple and adding the second tuple resultTuple= (*inputTuple_1,inputTuple_2) # Printing the result tuple # printing the resultant tuple after appending print("Resultant tuple after appending inputTuple_2 to the inputTuple_1:\n", resultTuple)

輸出

執行上述程式後,將生成以下輸出:

inputTuple_1: (12, 8, 6)
inputTuple_2: (3, 4)
Resultant tuple after appending inputTuple_2 to the inputTuple_1:
(12, 8, 6, (3, 4))

結論

在本文中,我們學習了四種不同的Python方法來將一個元組新增到另一個元組中。在本文中,我們還學習了list()、extend()和解包運算子(*)。在Python中,我們學習瞭如何將給定的元組轉換為列表,以及如何將列表轉換為元組。

更新於:2022年10月28日

10K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.