Python清单pop()函数返回和删除示例的示例教程

本文详细介绍了Python中列表的pop()函数,包括无索引参数的用法,弹出指定索引的元素,使用负索引以及处理索引超出范围的错误。通过实例展示了如何有效使用pop()进行列表操作。

Lists are an important part of the Python programming language. pop() function is used with lists and arrays in order to return the latest item from the list or array. In this tutorial we will learn how to use pop() function removes the latest item or specified item from a list.

列表是Python编程语言的重要组成部分。 pop()函数用于列表和数组,以便从列表或数组返回最新的项。 在本教程中,我们将学习如何使用pop()函数从列表中删除最新的项目或指定的项目。

pop()函数语法 (pop() Function Syntax)

pop() function has very simple syntax where we provide none or single argument.

pop()函数的语法非常简单,我们不提供任何参数或仅提供单个参数。

LIST.pop(INDEX)
  • `LIST` is the list, array or similar data type which stores multiple elements.

    LIST是存储多个元素的列表,数组或类似数据类型。
  • `INDEX` is the index number of the item we want to remove. If not provided the latest item will be returned and removed from the LIST. In python, the index numbers start from 0, not 1.

    “ INDEX”是我们要删除的商品的索引号。 如果未提供,则将返回最新项目并将其从LIST中删除。 在python中,索引号从0开始,而不是1。

The pop() function will return the item we want to pop. If an index number is provided which does not exist an error like IndexError: popo index out of range error will be returned.

pop()函数将返回我们要弹出的项目。 如果提供的索引号不存在,则会返回类似IndexError: popo index out of range的错误IndexError: popo index out of range error。

流行不带索引号 (Pop without and Index Number)

The most popular usage case is calling pop() function without index number. This will return the latest item of the given list or array. In this following example, we will run pop() function two times.

最流行的用法是调用没有索引号的pop()函数。 这将返回给定列表或数组的最新项。 在下面的示例中,我们将运行pop()函数两次。

mylist = ['Ankara','Istanbul','Canakale','London','Munih']
item = mylist.pop()

print(item)
# The output will be Munih
print(mylist)
# The output will be ['Ankara', 'Istanbul', 'Canakale', 'London']


item2 = mylist.pop()
print(item)

# The output will be Munih
print(mylist)
# The output will be ['Ankara', 'Istanbul', 'Canakale']
Pop without and Index Number 
Pop without and Index Number 
流行不带索引号

When we call the pop() function for the first time it returns the 'Munih' and we set the returned item into the variable named item. Then we list the current items of the mylist where the 'Munih' is removed.

当我们第一次调用pop()函数时,它将返回'Munih' ,并将返回的项设置为名为item的变量。 然后,我们列出的当前项目mylist其中'Munih'被删除。

LEARN MORE  How To Use Python Print Function Tutorial with Examples
了解更多如何通过示例使用Python打印功能教程

弹出给定的索引号(Pop For Given Index Number)

We can also use pop() function in order to return and remove a specific item whether the last item. We will provide the index number we want to return and remove from the given list. In this example, we will return and remove items with index number 2 and 3. Keep in mind that the index numbers start from 0 where 2nd item is 'Canakkale'.

我们还可以使用pop()函数以返回并删除特定项目(无论是最后一项)。 我们将提供要返回并从给定列表中删除的索引号。 在此示例中,我们将返回并删除索引号为2和3的项。请记住,索引号从0开始,其中第二项是'Canakkale'

mylist = ['Ankara','Istanbul','Canakale','London','Munih']

item = mylist.pop(2)

print(item)
#The output will be Canakale

print(mylist)
#The output will be ['Ankara', 'Istanbul', 'London', 'Munih']

用负索引号弹出 (Pop with Negative Index Number)

pop() function can be also used with negative index numbers. This can be useful if we want to return and remove an item in a reverse manner which is expressed as a negative number.

pop()函数也可以与负索引号一起使用。 如果我们想以相反的方式返回并删除一个表示为负数的项目,这将很有用。

>>> mylist = ['Ankara','Istanbul','Canakale','London','Munih']
>>> 
>>> item = mylist.pop(-2)
>>> 
>>> print(item)
London
>>> 
>>> print(mylist)
['Ankara', 'Istanbul', 'Canakale', 'Munih']
>>> 
>>> 
>>> 
>>> item = mylist.pop(-3)
>>> 
>>> print(item)
Istanbul
>>> 
>>> print(mylist)
['Ankara', 'Canakale', 'Munih']
>>>
Pop with Negative Index Number
Pop with Negative Index Number
用负索引号弹出

弹出索引超出范围错误(Pop Index Out Of Range Error)

Well while using index number with the pop() function we may get Index Out Of Range Error which is simply about trying to get an index number or item which does not exist. For example, if we try to get 7th item or 6th index number from a list which has only 4 items we will get Index Out Of Range Error.

好吧,当在pop()函数中使用索引号时,我们可能会得到“ Index Out Of Range Error ,这仅仅是关于尝试获取不存在的索引号或项目。 例如,如果我们尝试从只有4个项目的列表中获得第7个项目或第6个索引号,我们将获得Index Out Of Range Error

mylist = ['Ankara','Istanbul','Canakale','London']

item = mylist.pop(6)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: pop index out of range

item = mylist.pop(5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: pop index out of range

item = mylist.pop(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: pop index out of range

>>> item = mylist.pop(3)

print(item)
# The output will be London


Pop Index Out Of Range Error
Pop Index Out Of Range Error
弹出索引超出范围错误

翻译自: https://www.poftut.com/python-list-pop-function-to-return-and-remove-item-tutorial-with-examples/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值