日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

python list 刪除列表中某個元素的3種方法;附加刪除numpy數(shù)組中的指定索引元素的方法

作者:別出BUG求求了 更新時間: 2023-11-16 編程語言

方法

python中關(guān)于刪除list中的某個元素,一般有三種方法:

remove、pop、del

實例

1.remove: 刪除單個元素,刪除首個符合條件的元素,按值刪除
在這里插入圖片描述

2.pop: 刪除單個或多個元素,按位刪除(根據(jù)索引刪除)
在這里插入圖片描述
3.del:它是根據(jù)索引(元素所在位置)來刪除

在這里插入圖片描述

除此之外,del還可以刪除指定范圍內(nèi)的值。

在這里插入圖片描述
del 也可以刪除整個數(shù)據(jù)對象(列表、集合等)

在這里插入圖片描述
Traceback (most recent call last):
File “<pyshell#27>”, line 1, in
str
NameError: name ‘str’ is not defined

注意:del是刪除引用(變量)而不是刪除對象(數(shù)據(jù)),對象由自動垃圾回收機(jī)制(GC)刪除。

附加numpy的方法

如果是numpy數(shù)組,方法為:numpy.delete

import numpy as np
 
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
index = [2, 3, 6]
 
new_a = np.delete(a, index)
 
print(new_a) #Prints `[1, 2, 5, 6, 8, 9]`

原文鏈接:https://blog.csdn.net/weixin_39589455/article/details/128101558

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新