如何在Python中将一个元组附加到另一个元组?
在本文中,我们将向您展示如何在python中将一个元组附加到另一个元组。以下是完成此任务的各种方法 –
- 使用+运算符。
-
使用sum()函数。
-
使用list()和extend()函数。
-
使用拆包(*)运算符。
更多Python相关文章,请阅读:Python 教程
元组是一种不可变、无序的数据类型,用于在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中,我们学习了如何将给定的元组转换为列表和将列表转换为元组。