python基礎資料型別 (Elementary Data Type)——tuple

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.