新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:python列表添加和刪除的方法
1、添加元素

append():將單個(gè)元素添加到列表末尾
extend():將一個(gè)新列表拓展到原列表末尾
insert():將單個(gè)元素插入到列表指定位置
>>> li = ['one'] # li: ['one']
>>> li.append('two') # li: ['one', 'two']
>>> li.extend(['three','five']) # li: ['one', 'two', 'three', 'five']
>>> li.insert(3, 'four') # li: ['one', 'two', 'three', 'four', 'five']2、刪除元素
pop():刪除并返回列表的最后一個(gè)元素,也可以通過參數(shù)指定待刪除元素的索引
remove():刪除第一個(gè)能匹配參數(shù)值的元素,不返回內(nèi)容
clear() :清空整個(gè)列表,不返回內(nèi)容
>>> li = ['one', 'two', 'three', 'four', 'five']
>>> lastVal = li.pop() # li: ['one', 'two', 'three', 'four']
>>> firstVal = li.pop(0) # li: ['two', 'three', 'four']
>>> li.remove('three') # li: ['two', 'four']
>>> li.clear() # li: []以上就是python列表添加和刪除的方法,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
網(wǎng)站名稱:創(chuàng)新互聯(lián)Python教程:python列表添加和刪除的方法
網(wǎng)頁URL:http://www.dlmjj.cn/article/djsdpeo.html


咨詢
建站咨詢
