Python列表pop()方法

时间:2020-02-23 14:42:55  来源:igfitidea点击:

介绍

今天,我们将使用Python列表pop()方法。
通常,我们有各种内置方法可以从Python中的列表中删除任何元素。
我们有delremove()pop()方法来完成任务。
但是他们每个人都有自己的差异。
让我们找出如何使用pop()方法以及使用此方法有什么好处。

Python List pop()方法的工作

基本上,当没有传递参数时,Python中的pop()方法会弹出列表中的最后一项。
与某些索引一起传递时,该方法将弹出与该索引对应的元素。

语法,

#pop() method syntax in Python
pop(index)    

  • 传递索引后,该方法会删除索引处的元素,并返回相同的元素,
  • 如果不进行任何传递,则该方法将删除最后一个元素,并将其返回到先前调用该函数的位置。

使用Python列表pop()

看一下下面的示例代码,它说明了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)

在Python中列出pop()

  • 首先,我们将一个列表list1初始化为[0,1,2,3,4,5,6,7,8,9,10]。
    在此列表上,我们通过传递不同的索引来执行相应的弹出操作
  • pop()–如前所述,默认情况下pop()返回并从列表中删除最后一个元素。
    在我们的例子中,最后一个元素是10,它会连续弹出
  • pop(0)–弹出list1中位于第0个位置的元素,在本例中为0
  • 类似地,所有操作pop(1),pop(2),pop(3)和pop(4)都会在其各自的索引处返回项目。
    当我们继续将元素从列表中弹出时,它们是2 4 6和8。

使用Python列表pop()方法时发生错误

1.使用Python pop()的IndexError

在使用Python列表pop()方法时,如果传递给该方法的索引大于列表长度,则会遇到IndexError。

当索引提供的错误超出列表范围时,基本上会发生此错误。
让我们看一个小例子:

list1=["John","Charles","Alan","David"]

#when index passed is greater than list length
print(list1.pop(10))

输出:

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

在此示例中,很显然提供给pop()方法的索引10比列表的length(4)大。
因此,我们得到IndexError。

2.列表为空时出错

与上一节类似,当我们尝试在一个空列表上执行Python List pop()方法时,我们面临着相同的IndexError。
例如:

l1=[]
#for empty lists
print(l1.pop())

输出:

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

因此,我们可以得出结论,在对一个空列表执行Python list pop()方法时,将抛出IndexError。

因此,在应用pop()方法之前,必须检查要处理的列表是否为空。
简单的长度检查可以解决我们的问题。

l1=[]
#for empty lists check length before poping elements!
if len(l1)>0:
  print(l1.pop())
else:
  print("Empty list! cannot pop()")

输出:

Empty list! cannot pop()

Python中的if-else语句检查列表是否为空,并且仅当len(l1)> 0(即列表l1不为空)时才从列表中弹出元素。

Python堆栈上的Python列表pop()

正如我们在《 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)

  • 声明堆栈列表后,我们通过使用append()方法连续推送任务(元素)来推送5个元素。

  • 堆栈初始化完成后,我们将反复执行pop()元素,直到堆栈为空。

  • 注意,当从堆栈中弹出元素时,我们使用while循环使用了len(stack)> 0条件。
    这样可以确保仅在堆栈不为空时执行弹出操作。