Python基礎---容器Tuple

來源:互聯網
上載者:User

標籤:bsp   推導   pytho   strong   轉換   tuple   內建函數   --   最小   

元組Tuple

定義:容器內的元素不可變,該容器為元組

   使用 () 來表示一個元組

   元組在初始化後,其中的元素不可修改,不可刪除

建立元組:

  1、x = (obj1, obj2, obj3,...)  or  x = obj1, obj2, obj3, ...

1 x = (1, 2, 3, 4, 5)2 print(x, type(x))3 --->(1, 2, 3, 4, 5) <class ‘tuple‘>

 

  2、x = ()  建立一個空元組

1 x = ()2 print(x, type(x))3 --->() <class ‘tuple‘>

 

內建函數:

  1、len(tuple)  擷取tuple的長度

  2、max(tuple)  &  min(tuple)  擷取tuple的最大值和最小值

  3、tuple(seq)  將列錶轉換為tuple

1 list = [1, 2, 3, 4, 5]2 tup = tuple(list)3 print(tup)4 --->(1, 2, 3, 4, 5)

元組Tuple作為一個序列容器,和列表List一樣,具有:

  1、切片操作

  2、串連 +

  3、複製 *

  4、成員檢測  in & not in

  5、for ... in ...迴圈遍曆

 

元組推導式:

  tuple = (n for n in list if 判斷條件)

1 tup1 = (1, 2, 3, 4, 5)2 tup2 = (n * 2 for n in tup1)3 print(tup2, type(tup2))4 ---><generator object <genexpr> at 0x05672E70> <class ‘generator‘>

  經由元組推導式得到的這個元組,實際為一個產生器

 

Python基礎---容器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.