如何在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()**函式(返回iterable中所有項的總和)以連線兩個給定的元組。
列印將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()**函式(將序列/iterable轉換為列表)將兩個輸入元組轉換為列表。
使用**extend()函式**(將iterable(如列表、元組、字串等)的所有元素新增到列表的末尾)來將第二個列表新增到第一個列表中。
使用**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中,我們學習瞭如何將給定的元組轉換為列表,以及將列表轉換為元組。