python list()總結

來源:互聯網
上載者:User

標籤:方法   倒數   http   style   list   參數   差值   返回   檢索   

 

 

 

# 1 列表的建立,用方括弧表示[ ]name=[‘xiaolei‘,‘xiaoman‘,‘lixia‘,‘xiaolei‘]# 2 列表的查詢,通過索引值差值,第一位索引為0#倒數第一個print(name[-1])#倒數第二個print(name[-2])# name[name.index(xiaolei)],在name找到子字串的索引,然後在輸出name[0]# #----------#str -- 指定檢索的字串beg -- 開始索引,預設為0。end -- 結束索引,預設為字串的長度。print(name[name.index(‘xiaolei‘)])#統計重複出現的子字元的個數,用count()# str.count(sub, start= 0,end=len(string))# sub -- 搜尋的子字串# start -- 字串開始搜尋的位置。預設為第一個字元,第一個字元索引值為0。# end -- 字串中結束搜尋的位置。字元中第一個字元的索引為 0。預設為字串的最後一個位置。print(name.count(‘xiaolei‘))3 切片 [start:stop:step] 預設step為 1;-----切某段並返回,取某一段輸出------索引為 0到2,顧投不顧尾print(name[0:3])print(name[-3:])# 4 追加,從最後添加 append();插入,insert()insert() 函數用於將指定對象插入列表的指定位置。list.insert(index, obj)index -- 對象 obj 需要插入的索引位置。obj -- 要插入列表中的對象。該方法沒有傳回值,但會在列表指定位置插入對象。name1=[‘xiaolei‘,‘xiaoman‘,‘lixia‘,‘xiaolei‘]# 該方法沒有傳回值,但會在列表指定位置插入對象。#print(name1.insert(0,‘haha‘))name1.insert(0,‘haha‘)print(name1)# 5 修改,直接賦值name1[1]=‘xiaoya‘print(name1)# 6 刪除name2=[‘xiaolei‘,‘xiaoman‘,‘lixia‘,‘xiaolei‘]pop() 函數用於移除列表中的一個元素(預設最後一個元素),並且返回該元素的值。pow(index)print(name2.pop())remove() 函數用於移除列表中某個值的第一個匹配項。list.remove(obj)obj -- 列表中要移除的對象。#該方法 沒有傳回值 但是會移除列表中的某個值的--------第一個匹配項----------name3=[‘xiaolei‘,‘xiaoman‘,‘lixia‘,‘xiaolei‘]name3.remove(‘xiaolei‘)print(name3)del 全域性的指令,想刪誰就刪誰,刪除但不傳回值del 全域性的指令,想刪誰就刪誰,刪除但不傳回值name4=[‘xiaolei‘,‘xiaoman‘,‘lixia‘,‘xiaolei‘,‘haohao‘,‘1‘,‘2‘,‘3‘]#刪全域#del name4# print(name4)# 刪某個值del name4[0]print(name4)刪除某段區間, [start:stop:step] ,step預設為1,顧頭不顧尾包括start,stop實際取stop-1# # del name4[0:3]# # print(name4)#包括start,stop實際取stop-1del name4[0:6:2]print(name4)

 

7 迴圈name5=[‘xiaolei‘,‘xiaoman‘,‘lixia‘,‘xiaolei‘,‘haohao‘,‘1‘,‘2‘,‘3‘]#把列表裡面的每個值依次賦值給ifor i in name5:    print(i,end=‘ ‘)#python range() 函數可建立一個整數列表,一般用在 for 迴圈中。# range(start, stop[, step])# 參數說明:# start: 計數從 start 開始。預設是從 0 開始。例如range(5)等價於range(0, 5);# stop: 計數到 stop 結束,但不包括 stop。例如:range(0, 5) 是[0, 1, 2, 3, 4]沒有5# step:步長,預設為1。例如:range(0, 5) 等價於 range(0, 5, 1)range(start, stop[, step])for i in range(10):    #輸出0-9    print(i)# while 和 for迴圈的區別,while迴圈可以是死迴圈,for迴圈有邊界8 排序n=[‘a‘,‘e‘,‘b‘,‘c‘]print(n)#升序排列 sortn.sort()print(n)#降序排列n.reverse()print(n)9 兩個列表進行拼接直接相加+m1=[‘a‘,‘c‘‘f‘]m2=[‘z‘,‘x‘,‘c‘]直接相加+print(m1+m2)用extend()m1.extend(m2)print(m1)10 清除 所有 ------------clear()clear() 函數用於清空列表,類似於 del a[:]。list.clear()該方法沒有傳回值。複製 -----------------------copy()copy() 函數用於複製列表,類似於 a[:]。copy()方法文法:list.copy()返回複製後的新列表。a=[‘1‘,‘2‘,[‘x‘,‘y‘]]# 複製b=a.copy()print(a)print(b)# 修改原本列表元素a[0]=‘a‘print(a)print(b)# 修改原本列表子列表,子列表跟著改變a[2][0]=‘xx‘print(a)print(b)

python list()總結

聯繫我們

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