如何在 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 迴圈返回輸入列表的最後一個元素 -
# 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() 函式從列表中刪除最後一個元素。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP