700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Python中列表的del remove和pop函数之间的区别

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

时间:2023-05-07 00:13:14

相关推荐

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

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

delis nothing but"delete".delis 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)

removeis 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)

Thepop()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 thepop functionif 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 deletionprint('After deletion:',l) #removes the value 6 from listl.remove(6) # new list after removingprint('After removing:',l) #pop of the element at location 1 l.pop(1) # new list after popprint('After pop:',l)#pop of the element from the last of the listl.pop(-3) # new list after popprint('After pop:',l)#pop of the elements from the l.pop() # new list after popprint('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 listl.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 lastAnd,l.pop( )If not given any argument by default take that -1.

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。