python中pop函数_Python中的Pop函数

python中pop函数

什么是弹出功能? (What is the pop function?)

The method pop() removes and returns the last element from a list. There is an optional parameter which is the index of the element to be removed from the list. If no index is specified, a.pop() removes and returns the last item in the list. If the index passed to the pop() method is not in the range, it throws IndexError: pop index out of range exception.

方法pop()删除并返回列表中的最后一个元素。 有一个可选参数,它是要从列表中删除的元素的索引。 如果未指定索引,则a.pop()删除并返回列表中的最后一项。 如果传递给pop()方法的索引不在范围内,则会引发IndexError:pop index out of range异常。

用法示例 (Example Usage)

cities = ['New York', 'Dallas', 'San Antonio', 'Houston', 'San Francisco'];

print "City popped is : ", cities.pop()
print "City at index 2 is  : ", cities.pop(2)
输出量 (Output)
City popped is :  San Francisco
City at index 2 is  :  San Antonio

基本堆栈功能 (Basic Stack Functionality)

The pop() method is often used in conjunction with append() to implement basic stack functionality in a Python application.

pop()方法通常与append()结合使用,以在Python应用程序中实现基本的堆栈功能。

stack = []

for i in range(5):
    stack.append(i)

while len(stack):
    print(stack.pop())
更多信息: (More Information:)

The official documentation for pop() can be found here

pop()的官方文档可以在这里找到

翻译自: https://www.freecodecamp.org/news/pop-function-in-python/

python中pop函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值