可采用以下代码实现将元组拆分成两个部分,然后在需要使用的时候结合起来使用。
tuple1 = ('apple', 'banana', 'orange', 'pear') half_len = len(tuple1)//2 tuple1_first_half = tuple1[:half_len] tuple1_second_half = tuple1[half_len:]
print(tuple1_first_half) print(tuple1_second_half)
输出结果: ('apple', 'banana') ('orange', 'pear')
这样我们就将元组拆分成了两个部分,方便我们进行操作。