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

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。要访问列表中的一系列元素,必须对其进行切片。一种方法是使用简单的切片操作符,即 冒号(:)

通过这个操作符,可以定义开始切片的位置、结束切片的位置和步长。切片操作会从旧列表中创建一个新列表。

语法

List[ start : end : step]

参数

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()函数从列表中移除最后一个元素。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程