python列表删除某个元素

python中关于删除list中的某个元素,通常有三种方法:remove、pop、del:python

1.remove: 删除单个元素,删除首个符合条件的元素,按值删除
举例说明:shell

>>> str=[1,2,3,4,5,2,6]
>>> str.remove(2)
>>> str

[1, 3, 4, 5, 2, 6]app

 

2.pop: 删除单个或多个元素,按位删除(根据索引删除)ide

>>> str=[0,1,2,3,4,5,6]
>>> str.pop(1) #pop删除时有返回值,会返回被删除的元素
>>> str

[0, 2, 3, 4, 5, 6]对象

 

>>> str2=['abc','bcd','dce']
>>> str2.pop(2)

'dce'
>>> str2
['abc', 'bcd']索引

 

3.del:它是根据索引(元素所在位置)来删除
举例说明:rem

>>> str=[1,2,3,4,5,2,6]
>>> del str[1]
>>> str

[1, 3, 4, 5, 2, 6]it

 

>>> str2=['abc','bcd','dce']
>>> del str2[1]
>>> str2

['abc', 'dce']ast

 

除此以外,del还能够删除指定范围内的值。class

>>> str=[0,1,2,3,4,5,6]
>>> del str[2:4] #删除从第2个元素开始,到第4个为止的元素(可是不包括尾部元素)
>>> str

[0, 1, 4, 5, 6]

 

del 也能够删除整个数据对象(列表、集合等)

>>> str=[0,1,2,3,4,5,6]
>>> del str
>>> str #删除后,找不到对象

Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
str
NameError: name 'str' is not defined

注意:del是删除引用(变量)而不是删除对象(数据),对象由自动垃圾回收机制(GC)删除。

 

补充: 删除元素的变相方法

复制代码
s1 = (1, 2, 3, 4, 5, 6)
s2 = (2, 3, 5)
s3 = []
for i in s1:
    if i not in s2:
        s3.append(i)
print('s1_1:', s1)
s1 = s3
print('s2:', s2)
print('s3:', s3)
print('s1_2:', s1)
复制代码

 

python中关于删除list中的某个元素,通常有三种方法:remove、pop、del:

1.remove: 删除单个元素,删除首个符合条件的元素,按值删除
举例说明:

>>> str=[1,2,3,4,5,2,6]
>>> str.remove(2)
>>> str

[1, 3, 4, 5, 2, 6]

 

2.pop: 删除单个或多个元素,按位删除(根据索引删除)

>>> str=[0,1,2,3,4,5,6]
>>> str.pop(1) #pop删除时有返回值,会返回被删除的元素
>>> str

[0, 2, 3, 4, 5, 6]

 

>>> str2=['abc','bcd','dce']
>>> str2.pop(2)

'dce'
>>> str2
['abc', 'bcd']

 

3.del:它是根据索引(元素所在位置)来删除
举例说明:

>>> str=[1,2,3,4,5,2,6]
>>> del str[1]
>>> str

[1, 3, 4, 5, 2, 6]

 

>>> str2=['abc','bcd','dce']
>>> del str2[1]
>>> str2

['abc', 'dce']

 

除此以外,del还能够删除指定范围内的值。

>>> str=[0,1,2,3,4,5,6]
>>> del str[2:4] #删除从第2个元素开始,到第4个为止的元素(可是不包括尾部元素)
>>> str

[0, 1, 4, 5, 6]

 

del 也能够删除整个数据对象(列表、集合等)

>>> str=[0,1,2,3,4,5,6]
>>> del str
>>> str #删除后,找不到对象

Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
str
NameError: name 'str' is not defined

注意:del是删除引用(变量)而不是删除对象(数据),对象由自动垃圾回收机制(GC)删除。

 

补充: 删除元素的变相方法

复制代码
s1 = (1, 2, 3, 4, 5, 6)
s2 = (2, 3, 5)
s3 = []
for i in s1:
    if i not in s2:
        s3.append(i)
print('s1_1:', s1)
s1 = s3
print('s2:', s2)
print('s3:', s3)
print('s1_2:', s1)
复制代码
相关文章
相关标签/搜索