Python中列表的del,remove和pop函数之间的区别

1)Python del函数 (1) Python del function)

del is nothing but "delete". del is a keyword which basically goes on the position given by the user in del(position) and deletes that element and also changes the positions of all the other elements as it is now not the part of the list.

del只是“删除”而已。 del是一个关键字,它基本上位于用户在del(position)中给定的位置上,并删除该元素,并更改所有其他元素的位置,因为它现在不在列表中。

One import thing in delete is that it takes the argument in it is id i.e. not the whole data which is to be deleted only the location of the data.

delete中的一个重要事项是它接受参数中的id,即不是要删除的整个数据,而是仅数据的位置。

2)Python移除功能 (2) Python remove function)

remove is nothing only the searching of the first occurrence of the element and removes that element.

remove只是搜索元素的第一次出现并删除该元素。

Note: "remove" is slower than the "del" as it has to search the element which makes it slow.

注意: “删除”“ del”要慢,因为它必须搜索使其变慢的元素。

3)Python pop函数 (3) Python pop function)

The pop() only takes the single argument i.e the index and removes the element from there without affecting any others position. If we pass the index which was not in the range of the given list then it through the error i.e. "IndexError: pop index out of range".

pop()仅采用单个参数,即索引,并从那里删除元素,而不会影响其他位置。 如果我们传递的索引不在给定列表的范围内,则它会通过错误即“ IndexError:pop index out of range”出现错误。

It is not necessary to pass the argument in the pop function if not passed it takes it -1 by itself and remove the element from the last and delete that location from the list.

如果未传递参数,则无需在pop函数中传递参数,否则参数本身会使其为-1并从最后一个元素中删除该元素,然后从列表中删除该位置。

列表中del,remove和pop函数的Python示例 (Python example for del, remove and pop functions in the list)

l=[1,2,3,4,6,5,6,7,8,6] #list 
# del deletes the 4th position element i.e 6  
del(l[4])  
#new list after deletion
print('After deletion:',l)   

#removes the value 6 from list
l.remove(6) 
# new list after removing
print('After removing:',l)    

#pop of the element at location 1 
l.pop(1)    
# new list after pop
print('After pop:',l)     

#pop of the element from the last of the list
l.pop(-3)    
# new list after pop
print('After pop:',l)     

#pop of the elements from the 
l.pop() 
# new list after pop
print('After pop:',l)

Output

输出量

After deletion: [1, 2, 3, 4, 5, 6, 7, 8, 6]
After removing: [1, 2, 3, 4, 5, 7, 8, 6]
After pop: [1, 3, 4, 5, 7, 8, 6]
After pop: [1, 3, 4, 5, 8, 6]
After pop: [1, 3, 4, 5, 8]

Explanation of the code:

代码说明:

    In the above code,
    del(l[4])
    deletes the 4th position element  i.e. 6 of the list,
    and also change the position/location of all other further elements.
    
    And, 
    l.remove(6)
    Removes the element 6 from the list. 
    
    And, while using pop in list
    l.pop(1)
    Pops off  the first element of the list .
    
    And, 
    l.pop(-3)
    Pops off the 3rd element from the last 
    that means negative value means from last
    
    And,
    l.pop( )
    If not given any argument by default take that -1.


翻译自: https://www.includehelp.com/python/difference-between-del-remove-and-pop-functions-of-a-list-in-python.aspx

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值