Python 刪除列表中指定的元素

2021-09-25 22:27:44 字數 449 閱讀 8526

remove方法用於按值刪除元素,返回值為none;pop方法用於按下標刪除元素,返回值為刪除的元素。

# remove方法用於按值刪除元素,返回值為none;pop方法用於按下標刪除元素,返回值為刪除的元素。

l =[

'c',

'd',

'e',

'f']

print

(l)# 推薦

res = l.remove(

'e')

print

(l, res)

# 下面這種方法也可以,先找到下標,然後再呼叫pop方法,彈出指定下標的元素。

# 另外pop方法的預設索引為-1,即預設彈出最後乙個元素

res = l.pop(l.index(

'd')

)print

(l, res)

Python刪除字典中指定資料

pop 按鍵刪除並返回鍵所對應的值,如果鍵不存在,返回給定值,如果沒有給定值則會報錯 dict1 pop dict1.pop 1 print pop print dict1 輸出結果 1 鍵不存在的情況 dict1 pop dict1.pop 4,鍵不存在 print pop print dict1...

ES6刪除陣列中指定元

es6刪除陣列中指定元素,例 let array 1,2,3,4 刪除2,array.splice array.findindex item item 2 1 findindex 方法滿足條件時返回數元素下標,否則返回 1 splice index,len,item 刪除 item 不設定,inde...

刪除單鏈表中指定的節點

include stdafx.h include include include using namespace std struct node 建立單鏈表 node createsinglelist int arr,int n return head 輸出單鏈表 void print node h...