关于元组,元组和列表之间的转换

2023-06-13,,

注意元组是小括号,而且元祖只有两个功能count和index

>>> t=(1,2,3,4)

>>> dir(t)

['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']

>>> 

元祖转换成列表

>>> type(t)

<type 'tuple'>

>>> a=list(t)

>>> type(a)

<type 'list'>

列表转换成元祖

>>> a

[1, 2, 3, 4]

>>> type(a)

<type 'list'>

>>> t=tuple(a)

>>> t

(1, 2, 3, 4)

>>> type(t)

<type 'tuple'>

《关于元组,元组和列表之间的转换.doc》

下载本文的Word格式文档,以方便收藏与打印。