Python 如何将一个元组附加到另一个元组

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中,我们学习了如何将给定的元组转换为列表,以及将列表转换为元组。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程