本文共 6549 字,大约阅读时间需要 21 分钟。
python pop
Today we’ll be going the Python list pop() method. We generally have various built-in methods to remove or delete any element from a list in Python. We have del
, remove()
, as well as the pop()
method to accomplish the task. But each one of them has their own differences. Let’s find out how to use the pop() method and what are the benefits of using this method.
今天,我们将使用Python列表pop()方法。 通常,我们有各种内置方法可以从Python中的列表中删除或删除任何元素。 我们有del
, remove()
和pop()
方法来完成任务。 但是他们每个人都有自己的差异。 让我们找出如何使用pop()方法以及使用此方法有什么好处。
Basically, the pop()
method in Python pops out the last item in a list when no parameter is passed. When passed with some index, the method pops the element corresponding to the index.
基本上,当不传递任何参数时,Python中的pop()
方法会弹出列表中的最后一项。 与某些索引一起传递时,该方法将弹出与该索引对应的元素。
Syntax,
句法,
#pop() method syntax in Pythonpop(index)
Take a look at the example code below, it illustrates the use of the built-in pop()
method in python.
看一下下面的示例代码,它说明了python中内置pop()
方法的用法。
list1=[0,1,2,3,4,5,6,7,8,9,10]#pop last elementprint("pop() returns :",list1.pop(),"; currently list=",list1) #pop element at index 0print("pop(0) returns :",list1.pop(0),"; currently list=",list1)#pop element at index 1print("pop(1) returns :",list1.pop(1),"; currently list=",list1) #pop element at index 2print("pop(2) returns :",list1.pop(2),"; currently list=",list1) #pop element at index 3print("pop(3) returns :",list1.pop(3),"; currently list=",list1) #pop element at index 4print("pop(4) returns :",list1.pop(4),"; currently list=",list1)
Output:
输出:
While using the Python list pop() method, we encounter an IndexError if the index passed to the method is greater than the list length.
在使用Python列表pop()方法时,如果传递给该方法的索引大于列表长度,则会遇到IndexError 。
This Error occurs basically when the index provided it out of the list’s range. Let us look at a small example of this:
当索引提供的错误超出列表的范围时,基本上会发生此错误。 让我们看一个小例子:
list1=["John","Charles","Alan","David"]#when index passed is greater than list lengthprint(list1.pop(10))
Output:
输出 :
Traceback (most recent call last): File "C:/Users/sneha/Desktop/test.py", line 4, inprint(list1.pop(10))IndexError: pop index out of rangeProcess finished with exit code 1
In this example, it is clear that the index provided to the pop()
method, 10 is larger than the list’s length(4). Hence, we get the IndexError.
在此示例中,很明显提供给pop()
方法的索引10大于列表的length( 4 )。 因此,我们得到IndexError 。
Similar to the previous section, when we try to perform the Python List pop() method on an empty list, we face the same IndexError. For example:
与上一节类似,当我们尝试在一个空列表上执行Python List pop()方法时,我们面临着相同的IndexError 。 例如:
l1=[]#for empty listsprint(l1.pop())
Output:
输出 :
Traceback (most recent call last): File "C:/Users/sneha/Desktop/test.py", line 4, inprint(list1.pop())IndexError: pop from empty listProcess finished with exit code 1
So, we can conclude that while performing Python list pop() method on an empty list, an IndexError is thrown.
因此,我们可以得出结论,在对一个空列表执行Python list pop()方法时,将抛出IndexError 。
Hence, we must check before we apply the pop() method, that the list we are dealing with is not empty. A simple length check can solve our problem.
因此,在应用pop()方法之前,必须检查要处理的列表是否为空。 简单的长度检查可以解决我们的问题。
l1=[]#for empty lists check length before poping elements!if len(l1)>0: print(l1.pop())else: print("Empty list! cannot pop()")
Output:
输出 :
Empty list! cannot pop()
See, it’s easy. The checks whether the list is empty or not and only pops an element from the list when len(l1) > 0 i.e. when the list l1 is not empty.
看,很容易。 的检查列表是否为空,并且仅当len(l1)> 0(即列表l1不为空)时才从列表中弹出元素。
As we have seen in our , pop()
is also a stack operation used to remove the last task or element pushed. Let us see how we can implement the Python list pop() method in a stack using lists.
正如我们在所看到的, pop()
也是一个堆栈操作,用于删除最后推送的任务或元素。 让我们看看如何使用列表在堆栈中实现Python list pop()方法。
stack=[] #declare a stackprint("Pushing tasks into stack...")for i in range(5): stack.append(i)print("stack=",stack)print("Poping tasks from stack:")#performing repetitive pop() on stackwhile len(stack)>0: print("pop()=",stack.pop(),"; Currently in Stack:",stack)
Output:
输出 :
append()
method. 声明堆栈列表后,我们通过使用append()
方法连续推送任务(元素)来推送5个元素。 pop()
elements until the stack is empty. 堆栈初始化完成后,我们将反复pop()
元素,直到堆栈为空 。 In this tutorial, we learned how the built-in pop()
method in python works, errors related to it, as well as its applications in a stack. Feel free to ask any questions about the topic in the comments.
在本教程中,我们学习了python中内置的pop()
方法如何工作,与之相关的错误以及其在堆栈中的应用。 随时在评论中询问有关该主题的任何问题。
翻译自:
python pop
转载地址:http://qgozd.baihongyu.com/