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中的列表中删除删除任何元素。 我们有delremove()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:

输出:

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 consecutivelypop()–如前所述,默认情况下pop()返回并从列表中删除最后一个元素。 在我们的例子中,最后一个元素是10,它会连续弹出
  • pop(0) – This pops the element in the list1, at the 0th position, which is 0 in our casepop(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 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

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 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 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 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 stack
while len(stack)>0:print("pop()=",stack.pop(),";  Currently in Stack:",stack)

Output:

输出

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/11520540https://stackoverflow.com/questions/11520492/difference-between-del-remove-and-pop-on-lists/11520540
  • https://www.journaldev.com/16045/python-stackhttps://www.journaldev.com/16045/python-stack

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

python pop

python pop_Python清单pop()方法相关推荐

  1. python 字典 的pop 方法

    python 字典pop 方法的作用: 字典 pop() 方法删除字典给定键 key 及对应的值,返回值为被删除的值 字典pop 的语法: pop(key[,default]) demo 练习字典po ...

  2. Python set 的pop()方法 返回元素并不随机

    Python文档中对set的pop()方法描述: pop() 从集合中移除并返回任意一个元素. 如果集合为空则会引发 KeyError. 但实际上,调用set的pop()方法是某种顺序pop元素的. ...

  3. [转载] python 字典的get()pop()方法的区别

    参考链接: Python字典dictionary| pop方法 get()方法 返回指定键 key 的值,如果值不在字典中返回默认值.语法:dict.get(key, default=None) 参数 ...

  4. [转载] python中字典中追加_python 中字典中的删除,pop 方法与 popitem 方法

    参考链接: Python字典popitem() 1.pop 方法:删除指定的键值对,最后返回的是删除键的值. 2.popitem 方法:每次删除字典中的最后一个键值对,返回这个删除的键值对. 3.cl ...

  5. python字典中删除键值对的del语句与pop方法

    删除字典中的键值对的有del语句与pop方法,del 语句和 pop() 函数作用相同,pop() 函数有返回值,返回值为对应的值,del语句没有返回值.pop()函数语法:pop(key[,defa ...

  6. python x pop,Python Set pop() 方法

    我在学习过程中发现, set 集合的 pop() 方法, 不像上面所述的那样, 只是随机删除一个元素, 而是有一定的规律可循的, 我将我发现的规律总结如下: # 执行下面的代码,并查看输出结果: pr ...

  7. [转载] python 需求清单_Python清单操作摘要

    参考链接: Python清单 python 需求清单 列出功能和方法,理解和性能特征 (List Functions & Methods, Comprehension and Performa ...

  8. python 技能清单_Python清单

    python 技能清单 Today we going to learn about Python List. Earlier we learnt about Python Numbers which ...

  9. 刻意练习:Python基础 -- Task11. 魔法方法

    背景 我们准备利用17天时间,将 "Python基础的刻意练习" 分为如下任务: Task01:变量.运算符与数据类型(1day) Task02:条件与循环(1day) Task0 ...

最新文章

  1. 怎么用u盘在服务器上传文件,U盘向云服务器传输文件吗
  2. 关于计算机和人物的英语短文,人脑和电脑英语作文
  3. BASIC-5 查找整数
  4. Android 可拖拽的GridView效果实现, 长按可拖拽和item实时交换
  5. oracle9i 恢复数据库,Oracle9i RMAN备份及恢复步骤(zt)
  6. 手把手教你如何利用Kickstart自动安装虚拟机
  7. The Unsolvable Problem
  8. android textview 字体边框,为TextView添加一个边框的几种办法
  9. saved_model_cli查看SavedModel
  10. 【OpenCV】尺寸测量
  11. Class6 基于ECS和NAS搭建个人网盘
  12. 关于紫边、紫晕、Color shading成因的总结
  13. MATLAB 全景图切割及盒图显示
  14. CMDN CLUB第14场:小米与友盟专家详解Android开发:
  15. 哈工大计算机系统lab7——微壳
  16. 专业计算机术语中英文对照(二)
  17. stm32点亮LED的有关寄存器配置CRL、CRH、IDR、ODR
  18. ui设计发展到底好不好?为什么越来越多的人开始学习UI设计?
  19. Success diary
  20. 手机sim卡被格式化了数据怎么找回来

热门文章

  1. 查看Linux系统版本的命令总结
  2. 搜索网站/论坛内容帖子
  3. Java4班题库-传智专修学院Java面试题库二
  4. 计算机硬件系统和操作系统
  5. LTE系统名词解释及上下行过程
  6. 回顾 | Teams + Power Platform低代码数字化技术融合趋势
  7. 使用AES加密进行前端加、解密
  8. bzoj1069: [SCOI2007]最大土地面积 凸包+旋转卡壳求最大四边形面积
  9. arch linux u盘安装,从U盘安装archlinux-2009.08完整过程 - Leo's Utopia
  10. aps是什么意思_APS系统是什么意思?起什么作用