這篇文章主要介紹關(guān)于Python之列表增刪改查的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
成都創(chuàng)新互聯(lián)公司成都企業(yè)網(wǎng)站建設(shè)服務(wù),提供成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)網(wǎng)站開發(fā),網(wǎng)站定制,建網(wǎng)站,網(wǎng)站搭建,網(wǎng)站設(shè)計(jì),成都響應(yīng)式網(wǎng)站建設(shè)公司,網(wǎng)頁設(shè)計(jì)師打造企業(yè)風(fēng)格網(wǎng)站,提供周到的售前咨詢和貼心的售后服務(wù)。歡迎咨詢做網(wǎng)站需要多少錢:13518219792
列表的查
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] print(a[1:4])
結(jié)果:
['tom', 'jack', 'james']
注意:
列表的切片的下標(biāo)以 0 為開始。即: 下標(biāo)0 --> klvchen,下標(biāo)1 --> tom,..... 下標(biāo)5 --> lucy。
列表的切換顧頭不顧尾,例如:a [1:4],則不會(huì)取到列表下標(biāo)為4的元素
從左到右,取到最后:
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] print(a[1:])
結(jié)果:
['tom', 'jack', 'james', 'lily', 'lucy']
從左到右,取到倒數(shù)第二個(gè)值:
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] print(a[1:-1])
結(jié)果:
['tom', 'jack', 'james', 'lily']
從左到右,隔一個(gè)去?。?/p>
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] print(a[1::2])
結(jié)果:
['tom', 'james', 'lucy']
從右到左,一個(gè)一個(gè)去?。?/p>
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] print(a[3::-1])
結(jié)果:
['james', 'jack', 'tom', 'klvchen']
從右到左去取
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] print(a[-2:-4:-1])
結(jié)果:
['lily', 'james']
列表的增
append 方法默認(rèn)插到最后一個(gè)位置
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] a.append("mary") print(a)
結(jié)果:
['klvchen', 'tom', 'jack', 'james', 'lily', 'lucy', 'mary']
insert 方法將數(shù)據(jù)插入到任意一個(gè)位置
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] a.insert(1,"mary") print(a)
結(jié)果:
['klvchen', 'mary', 'tom', 'jack', 'james', 'lily', 'lucy']
extend 方法可在列表的末尾一次性追加另一個(gè)序列中的多個(gè)值
a = ["klvchen", "tom", "jack"] b = ["james", "lily", "lucy"] a.extend(b) print(a)
結(jié)果:
['klvchen', 'tom', 'jack', 'james', 'lily', 'lucy']
列表的改
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] a[1]="tom2" print(a)
結(jié)果:
['klvchen', 'tom2', 'jack', 'james', 'lily', 'lucy'] a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] a[1:3]=["tom2", "jack2"] print(a)
結(jié)果:
['klvchen', 'tom2', 'jack2', 'james', 'lily', 'lucy']
列表的刪
remove 方法
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] a.remove("tom") print(a)
結(jié)果:
['klvchen', 'jack', 'james', 'lily', 'lucy']
pop 方法
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] b = a.pop(1) print(a) print(b)
結(jié)果:
['klvchen', 'jack', 'james', 'lily', 'lucy'] tom
注意 pop 方法會(huì)返回刪除的元素
del 方法
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] del a[1] print(a)
結(jié)果:
['klvchen', 'jack', 'james', 'lily', 'lucy'] a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] del a print(a)
結(jié)果:
NameError: name 'a' is not defined
注意:del a 把列表a從內(nèi)存刪除了,所以打印 a 列表程序會(huì)報(bào)錯(cuò)
列表元素的統(tǒng)計(jì)
list = ['a', 'a', 'b', 'c'] res = list.count('a') print(res)
結(jié)果:
2
列表的排序
sort 方法
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] print(a) a.sort() print(a)
結(jié)果:
['klvchen', 'tom', 'jack', 'james', 'lily', 'lucy'] ['jack', 'james', 'klvchen', 'lily', 'lucy', 'tom']
reverse 方法
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] print(a) a.reverse() print(a)
結(jié)果:
['klvchen', 'tom', 'jack', 'james', 'lily', 'lucy'] ['lucy', 'lily', 'james', 'jack', 'tom', 'klvchen']
以上是關(guān)于Python之列表增刪改查的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!