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']

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'
被删除。
弹出给定的索引号(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 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

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