python基礎 列表 的使用

來源:互聯網
上載者:User

標籤:sort   lis   pre   [1]   五個   長度   輸出   ever   word   

列表

首先定義一個列表

聲明列表

列表名字=[值1,值2]

list=[1,2,3,4,5]

這是一個列表,列表中有五個元素(1,2,3,4,5)

顯示list列表

print   list

輸出  ——[1,2,3,4,5]

列表的下標是從0開始的

下標=列表的長度-1

print  list[1:3]

輸出 ——[2,3]

 

print list[2]

輸出——[3]

 

print list[1:]

輸出 ——[]

 

接下來我們試試

列表中的一些方法

 

在列表末尾追加一個元素

列表名.append(元素)

list.append(需要追加的元素)

list.append(6)

print list

輸出 ——[1,2,3,4,5,6]

 

Max(),Min(),輸出最大值,最小值,

print Max(list)

輸出——[6]

print Min(list)

輸出——[1]

 

len(列表名)           #顯示列表長度

print len(list)

輸出——6

 

刪除元素

del(列表名[刪除元素下標])

del(list[1])

輸出——[1,3,4,5,6]

 

輸出資料行表中某元素個數

列表名.count(元素)

 

將一個列表追加到另一個列表

list=[1,2,3,4,5,6]

list1=[7,8,9,10]

list.extend.list1

print list

輸出——[1,2,3,4,5,6,7,8,9,10]

 

尋找位置

list=[hello,word,lost]

list.index(hello)

輸出——0

 

將對象插入列表

insert(索引位置,插入對象)

list.insert(2,3)

print list

輸出——[hello,word,3,lost]

 

將列表反轉

list.reverse()

print list

輸出——[lost,3,word,hello]

 

將列表排序

list=[2,5,4,8,3,1]

list.sort()

print  list

輸出——[1,2,3,4,5,8]

 

列表寫到這裡  大家不喜勿噴!!!

謝謝!

 

 

 

 

 

 

 

 


python基礎 列表 的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.