can元组包含元组作为元素

时间:2018-01-06 05:46:50

标签: python

Python列表可以包含list和tuple作为元素,因此元组是否可以包含元组作为成员?

 >>> lst1 = [1, 3, 5]
 >>> lst2 = [2, 4, 6]
 >>> tup1 = (9, 7, 5)
 >>> tup2 = (8, 6, 4)
 >>> clist = [lst1, lst2, tup1, tup2]
 >>> print(clist)
 [[1, 3, 5], [2, 4, 6], (9, 7, 5), (8, 6, 4)]

1 个答案:

答案 0 :(得分:3)

是的,元组可以包含元组作为成员。语法如下:

(1, 2, (3, 4), 5)

或者也许:

tup2 = (3, 4)
tup1 = (1, 2, tup2, 5)