Python|如何将元组连接到嵌套元组
有时,在使用元组时,我们可能会遇到一个问题,即我们需要将单个记录转换为嵌套集合,但仍保留为单独的元素。通常添加元组,通常会添加内容并因此使生成的容器变平,这通常是不希望的。让我们讨论解决这个问题的某些方法。
方法#1:在初始化期间使用+ operator
+ ", " operator
在这个方法中,我们执行元组元素的通常添加,但是在初始化元组时,我们在元组后面添加一个逗号,这样它们在添加时不会变平。
# Python3 code to demonstrate working of
# Concatenating tuples to nested tuples
# using + operator + ", " operator during initialization
# initialize tuples
test_tup1 = (3, 4),
test_tup2 = (5, 6),
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Concatenating tuples to nested tuples
# using + operator + ", " operator during initialization
res = test_tup1 + test_tup2
# printing result
print("Tuples after Concatenating : " + str(res))
输出 :
The original tuple 1 : ((3, 4), )
The original tuple 2 : ((5, 6), )
Tuples after Concatenating : ((3, 4), (5, 6))
方法#2:在连接过程中使用“,”运算符
此任务也可以通过在连接期间应用“,”运算符来执行。它可以执行安全连接。
# Python3 code to demonstrate working of
# Concatenating tuples to nested tuples
# Using ", " operator during concatenation
# initialize tuples
test_tup1 = (3, 4)
test_tup2 = (5, 6)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Concatenating tuples to nested tuples
# Using ", " operator during concatenation
res = ((test_tup1, ) + (test_tup2, ))
# printing result
print("Tuples after Concatenating : " + str(res))
输出 :
The original tuple 1 : ((3, 4), )
The original tuple 2 : ((5, 6), )
Tuples after Concatenating : ((3, 4), (5, 6))