如何在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.