天天看點

關于元組,元組和清單之間的轉換

注意元組是小括号,而且元祖隻有兩個功能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]

>>> t=tuple(a)

>>> t

(1, 2, 3, 4)

繼續閱讀