標籤:object recent error support 迭代 列表 建立 trace kernel
一、元組的建立與轉換:
ages = (11, 22, 33, 44, 55)ages = tuple((11, 22, 33, 44, 55))
ages = tuple([]) # 字串、列表、字典(預設是key)
- tuple(iterable),可以存放所有可迭代的資料類型
二、元組的不可變性
如:t = (17, ‘Jesse‘, (‘LinuxKernel‘, ‘Python‘), [17, ‘Jesse‘])
元組t中的元素數字17和字串‘Jesse’以及元組(‘LinuxKernel‘, ‘Python‘)本身屬於不可變元素,故其在元組中不可更新;但是其中包含的列表[17, ‘Jesse‘]本身屬於可變元素,故:
>>> t = (17, ‘Jesse‘, (‘LinuxKernel‘, ‘Python‘), [17, ‘Jesse‘])>>> t(17, ‘Jesse‘, (‘LinuxKernel‘, ‘Python‘), [17, ‘Jesse‘])>>> t[0] = 18Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: ‘tuple‘ object does not support item assignment>>> t[3][17, ‘Jesse‘]>>> t[3][0] = 21>>> t(17, ‘Jesse‘, (‘LinuxKernel‘, ‘Python‘), [21, ‘Jesse‘])
三、元組常用操作
#count(self,value)#功能:統計當前元組中某元素的個數tup = (55,77,85,55,96,99,22,55,)tup.count(55)#返回結果:3 元素‘55’在元組tup中出現了3次#index(self, value, start=None, stop=None)功能:擷取元素在元組中的索引值,對於重複的元素,預設擷取從左起第一個元素的索引值tup = (55,77,85,55,96,99,22,55,)tup.index(55)返回結果:0tup.index(85)返回結果:2tup.index(55,2,7)返回結果:3
tuple源碼
python基礎資料型別 (Elementary Data Type)——tuple