天天看點

python pop_Python清單pop()方法

python pop

介紹 (Introduction)

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()方法以及使用此方法有什麼好處。

Python List pop()方法的工作 (Working of Python List pop() Method)

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 Python
pop(index)    
           
  • When an index is passed, the method removes the element at the index, as well as returns the same,

    傳遞索引後 ,該方法将删除索引處的元素,并傳回該元素,

  • When nothing is passed, the method removes the last element and returns it where the function was previously called.

    如果沒有傳遞任何内容 ,則該方法将删除最後一個元素,并将其傳回到先前調用該函數的位置。

使用Python清單pop() (Using the Python List pop())

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 element
print("pop() returns :",list1.pop(),"; currently list=",list1)   

#pop element at index 0
print("pop(0) returns :",list1.pop(0),"; currently list=",list1)

#pop element at index 1
print("pop(1) returns :",list1.pop(1),"; currently list=",list1)  

#pop element at index 2
print("pop(2) returns :",list1.pop(2),"; currently list=",list1)    

#pop element at index 3
print("pop(3) returns :",list1.pop(3),"; currently list=",list1) 

#pop element at index 4
print("pop(4) returns :",list1.pop(4),"; currently list=",list1)

           

Output:

輸出:

python pop_Python清單pop()方法

List pop() In Python 在Python中列出pop()

  • At first, we initialize a list, list1 as [0,1,2,3,4,5,6,7,8,9,10]. On this list, we perform the corresponding pop operation by passing distinct indices

    首先,我們将list1初始化為[0,1,2,3,4,5,6,7,8,9,10]。 在此清單上,我們通過傳遞不同的索引來執行相應的pop操作

  • pop() – As stated earlier, by default pop() returns and removes the last element from a list. In our case, the last element was 10, which gets popped consecutively

    pop()–如前所述,預設情況下pop()傳回并從清單中删除最後一個元素。 在我們的例子中,最後一個元素是10,它會連續彈出

  • pop(0) – This pops the element in the list1, at the 0th position, which is 0 in our case

    pop(0)–彈出list1中位于第0個位置的元素,在本例中為0

  • Similarly, all the operations pop(1), pop(2), pop(3), and pop(4) return the items at their respective indices. Which are 2 4 6 and 8 as we continue to pop elements out of the list.

    類似地,所有操作pop(1),pop(2),pop(3)和pop(4)都會在其各自的索引處傳回項目。 當我們繼續将元素從清單中彈出時,它們是2 4 6和8。

使用Python清單pop()方法時發生錯誤 (Errors while using Python List pop() Method)

1.使用Python pop()的IndexError (1. IndexError with Python pop())

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 length
print(list1.pop(10))
           

Output:

輸出 :

Traceback (most recent call last):
  File "C:/Users/sneha/Desktop/test.py", line 4, in <module>
    print(list1.pop(10))
IndexError: pop index out of range

Process 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 。

2.清單為空時出錯 (2.Error when the list is empty)

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 lists
print(l1.pop())
           

Output:

輸出 :

Traceback (most recent call last):
  File "C:/Users/sneha/Desktop/test.py", line 4, in <module>
    print(list1.pop())
IndexError: pop from empty list

Process 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 if-else statement in Python 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.

看,很容易。 Python中的if-else語句檢查清單是否為空,并且僅當len(l1)> 0(即清單l1不為空)時才從清單中彈出元素。

Python堆棧上的Python清單pop() (Python List pop() on a Python Stack)

As we have seen in our Python Stack Tutorial,

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.

正如我們在Python Stack Tutorial中所看到的,

pop()

也是一個堆棧操作,用于删除最後推送的任務或元素。 讓我們看看如何使用清單在堆棧中實作Python list pop()方法。

stack=[] #declare a stack

print("Pushing tasks into stack...")
for i in range(5):
    stack.append(i)

print("stack=",stack)

print("Poping tasks from stack:")
#performing repetitive pop() on stack
while len(stack)>0:
    print("pop()=",stack.pop(),";  Currently in Stack:",stack)
           

Output:

輸出 :

python pop_Python清單pop()方法

Pop On Stack 彈出堆棧

  • After declaring a stack list, we push 5 elements by continuously pushing tasks(elements) using the

    append()

    method.

    聲明堆棧清單後,我們通過使用

    append()

    方法連續推送任務(元素)來推送5個元素。
  • As soon as our stack initialization is done, we repetitively

    pop()

    elements until the stack is empty.

    堆棧初始化完成後,我們将反複

    pop()

    元素,直到堆棧為空 。
  • Notice that while poping elements from the stack we have used the condition len(stack) > 0 using the while loop. This ensures that the pop operation is performed only while the stack is not empty.

    注意,當從堆棧中彈出元素時,我們使用while循環使用了len(stack)> 0條件。 這樣可確定僅在堆棧不為空時執行彈出操作。

結論 (Conclusion)

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

方法如何工作,與之相關的錯誤以及其在堆棧中的應用。 随時在評論中詢問有關該主題的任何問題。

參考資料 (References)

  • https://stackoverflow.com/questions/11520492/difference-between-del-remove-and-pop-on-lists/11520540

    https://stackoverflow.com/questions/11520492/difference-between-del-remove-and-pop-on-lists/11520540

  • https://www.journaldev.com/16045/python-stack

    https://www.journaldev.com/16045/python-stack

翻譯自: https://www.journaldev.com/36155/python-list-pop-method

python pop