简体   繁体   English

如何在Python中嵌套元组

[英]How to nest tuples in Python

I am very new at Python. 我在Python的非常新。 I am trying to do a tuple that contains tuples in the form (1,(1,(1,'a'))) . 我正在尝试做一个包含(1,(1,(1,'a')))形式的元组的元组。 I am not allowed to use any functions. 我不允许使用任何功能。 I have written a list and for each element of the list I want to take tuple. 我已经写了一个列表,并且我想为列表的每个元素取元组。

I want to take something like (2,(3,(4,'name'))) and the result I take is (2,3,4,'name') . 我想要采取类似(2,(3,(4,'name'))) ,我得到的结果是(2,3,4,'name')

b = [6,5,4,3,2,1]
for i in range(len(b)):
mytuple = (i,'name')
print(i)
mytuple = (b[2],)+mytuple
print(mytuple)

Iterate reverse on b: 在b上反向迭代:

t="name"                                                                                                           
for i in range(-1,-len(b)-1,-1): 
       t=(b[i],t) 

Out: (6, (5, (4, (3, (2, (1, 'name'))))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM