如何在 Python 中获取列表的最后一个元素?


在本文中,我们将向您展示如何使用 Python 获取输入列表的最后一个元素。现在,我们来看 4 种方法来完成此任务:

  • 使用负索引

  • 使用切片

  • 使用 pop() 方法

  • 使用 For 循环

假设我们已经获取了一个包含一些元素的列表。我们将使用上面指定的不同方法返回给定输入列表的最后一个元素。

方法 1:使用负索引

Python 允许“从末尾开始索引”,即负索引。

这意味着序列中的最后一个值的索引为 -1,倒数第二个的索引为 -2,依此类推。

当您想从可迭代对象末尾(右侧)选择值时,您可以利用负索引来获得优势。

语法

list[len − 1]: By definition, points to the last element.
list[−1]: Negative indexing starting from the end

算法(步骤)

以下是执行所需任务应遵循的算法/步骤:

  • 创建一个变量来存储输入列表

  • 使用“**列表长度 - 1**”作为索引获取列表的最后一个元素,并打印所得的列表的最后一个元素。

  • 使用**-1**(负索引)作为索引获取列表的最后一个元素,并打印所得的列表的最后一个元素。

示例

以下程序使用负索引返回输入列表的最后一个元素:

# input list inputList = [5, 1, 6, 8, 3] # printing input list print("Input list:", inputList) # getting the last element of the list using the length of the list - 1 as an index(Here list index starts from 0 so list length -1 gives the index of the last element of the list) print("Last element of the input list using len(list)-1 as index = ", inputList[len(inputList) - 1]) # getting the last element of the list using - 1 as the index print("Last element of the input list using -1 as index = ", inputList[-1])

输出

执行上述程序后,将生成以下输出:

Input list: [5, 1, 6, 8, 3]
Last element of the input list using len(list)-1 as index = 3
Last element of the input list using -1 as index = 3  

方法 2:使用切片

列表切片是 Python 中的常见做法,它是程序员解决有效问题的最常用方法。考虑一个 Python 列表。要访问列表中的一系列元素,您必须对其进行切片。一种方法是使用简单的切片运算符,即**冒号 (:)**

使用此运算符,可以定义切片的起始位置、结束位置和步长。列表切片从旧列表创建一个新列表。

语法

List[ start : end : step]

参数

**开始** - 从哪里开始索引

**结束** - 结束索引

**步长** - 在两者之间采取的跳跃次数,即步长

算法(步骤)

以下是执行所需任务应遵循的算法/步骤:

  • 使用**切片**获取输入列表的最后一个元素,并创建一个变量来存储它。

inputList[-1:][0] represents getting the first element by slicing it from the end of the list   
  • 打印输入列表的最后一个元素。

示例

以下程序使用切片返回输入列表的最后一个元素:

# input list inputList = [5, 1, 6, 8, 3] # printing input list print("Input list:", inputList) # getting the last element of the list using slicing lastElement = inputList[-1:][0] # printing the last element of the list print("Last element of the input list = ", lastElement)

输出

Input list: [5, 1, 6, 8, 3]
Last element of the input list = 3

方法 3:使用 pop() 方法

算法(步骤)

以下是执行所需任务应遵循的算法/步骤:

  • 给出输入列表

  • 使用 **pop() 函数**(从列表中删除最后一个元素并返回它),以获取列表的最后一个元素

  • 打印所得的列表的最后一个元素。

示例

以下程序使用 pop() 函数返回输入列表的最后一个元素:

# input list inputList = [5, 1, 6, 8, 3] # printing input list print("Input list:", inputList) # getting the last element of the list using pop() method print("Last element of the input list = ", inputList.pop())

输出

Input list: [5, 1, 6, 8, 3]
Last element of the input list = 3

方法 4:使用 For 循环

算法(步骤)

以下是执行所需任务应遵循的算法/步骤:

  • 使用 for 循环,使用 **len() 函数**(len() 方法返回对象中的项目数)遍历列表的长度。

  • 使用 if 条件语句,检查迭代器值是否等于列表长度 -1。

  • 如果条件为真,则打印列表的对应元素。

示例

以下程序使用 for 循环返回输入列表的最后一个元素:

# input list inputList = [5, 1, 6, 8, 3] # printing input list print("Input list:", inputList) # Traversing till the length of the list for k in range(0, len(inputList)): # Checking whether the iterator value is equal to the length of list -1(last element) # Here == is used to check whether the iterator value is equal to list length-1 if k == (len(inputList)-1): # Printing the corresponding element of the list, # if the condition is true print("Last element of the input list = ", inputList[k])

输出

Input list: [5, 1, 6, 8, 3]
Last element of the input list = 3

结论

在本文中,我们学习了如何使用四种不同的方法获取列表的最后一个元素:负索引、pop() 函数、负索引和循环。我们还学习了如何使用 pop() 函数从列表中删除最后一个元素。

更新于:2023-08-23

81K+ 浏览量

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.