使用 Python 刪除列表中大於特定值的元素


在本文中,我們將學習如何在 Python 中從列表中刪除大於特定值的元素。

使用的方法

以下是完成此任務使用的各種方法 -

  • 使用 remove() 方法

  • 使用列表推導式

  • 使用 filter() 方法和 lambda 函式

方法 1:使用 remove() 方法

remove() 函式(從列表中刪除元素的第一次出現)

演算法(步驟)

以下是執行所需任務的演算法/步驟 -

  • 建立一個變數來儲存輸入列表

  • 建立另一個變數來儲存另一個輸入值

  • 使用for 迴圈迭代輸入列表中的每個元素。

  • 使用if 條件語句檢查當前元素是否大於指定的輸入值。

  • 如果條件為真,則使用 to remove() 函式透過將其作為引數傳遞給它來從列表中刪除該當前元素。

  • 刪除大於指定輸入值的元素後,列印結果列表。

示例

以下程式使用 remove() 函式從列表中刪除大於指定輸入值的元素 -

# input list
inputList = [45, 150, 20, 90, 15, 55, 12, 75]
# Printing the given list
print("The Given list is:", inputList)
# input value
inputValue = 50
# iterarting through the list
for i in inputList:
   # checking whether the current element is greater than the input value
   if i > inputValue:
      # removing that current element from the list if the condition is true
      inputList.remove(i)
# printing the resultant list after removing elements larger than 50
print("Removing elements larger than 50 from the list:\n", inputList)

輸出

執行後,以上程式將生成以下輸出 -

The Given list is: [45, 150, 20, 90, 15, 55, 12, 75]
Removing elements larger than 50 from the list:
 [45, 20, 15, 12]

方法 2:使用列表推導式

列表推導式

當您希望基於現有列表的值構建新列表時,列表推導式提供了一種更短/簡潔的語法。

示例

以下程式使用列表推導式從輸入列表中刪除大於指定輸入值的元素 -

# input list
inputList = [45, 150, 20, 90, 15, 55, 12, 75]
# Printing the given list
print("The Given list is:", inputList)

# input value
inputValue = 50

# removing elements from a list larger than 50
# by traversing through the list and storing elements
# that are having a value less than or equal to the given input value
resultList = [k for k in inputList if k <= inputValue]

# printing the resultant list
print("Removing elements larger than 50 from the list:", resultList)

輸出

執行後,以上程式將生成以下輸出 -

The Given list is: [45, 150, 20, 90, 15, 55, 12, 75]
Removing elements larger than 50 from the list: [45, 20, 15, 12]

方法 3:使用 filter() 方法和 lambda 函式

Lambda 函式

Lambda 函式,通常稱為“匿名函式”,與普通 Python 函式相同,只是它可以無需名稱定義。def 關鍵字用於定義普通函式,而lambda 關鍵字用於定義匿名函式。但是,它們僅限於一行表示式。它們與常規函式一樣,可以接受多個引數。

語法

lambda arguments: expression
  • 此函式接受任意數量的輸入,但僅評估並返回一個表示式。

  • Lambda 函式可以在需要函式物件的地方使用。

  • 您必須記住,lambda 函式在語法上僅限於單個表示式。

演算法(步驟)

以下是執行所需任務的演算法/步驟 -

  • 使用 lambda 函式檢查可迭代物件的每個元素。

  • 使用 filter() 函式過濾所有值小於給定輸入值的元素。

  • filter() 函式 - 使用一個函式過濾指定的序列,該函式確定序列中每個元素是真還是假。

  • 使用 list() 函式將此過濾器物件轉換為列表。

  • 刪除大於指定輸入值的元素後,列印結果列表。

示例

以下程式使用 filter() 和 lambda() 函式從輸入列表中刪除大於指定輸入值的元素 &miinus;

# input list
inputList = [45, 150, 20, 90, 15, 55, 12, 75]
print("The Given list is:", inputList)
# input value
inputValue = 50

# Filtering list objects that are having value
# less than or equal to the given input Value
filteredObject = filter(lambda k: k <= inputValue, inputList)

# Convert the filter object to a list using the list() function
resultList = list(filteredObject)

# printing the resultant list after removing elements larger than 50
print("Removing elements larger than 50 from the list:\n", resultList)

輸出

執行後,以上程式將生成以下輸出 -

The Given list is: [45, 150, 20, 90, 15, 55, 12, 75]
Removing elements larger than 50 from the list:
 [45, 20, 15, 12]

方法 4:使用 for 迴圈和 append() 函式

示例

以下程式使用 for 迴圈和 append() 函式從輸入列表中刪除大於指定輸入值的元素 -

# input list
inputList = [45, 150, 20, 90, 15, 55, 12, 75]
print("The Given list is:", inputList)
# input value
inputValue = 50
# Creating an empty list to store the result
resultList = []
# iterarting through the list
for i in inputList:
   # checking whether the current element is less than or equal to the input value
   if i <= inputValue:
      # add this element to the result list
      resultList.append(i)
# printing the resultant list after removing elements larger than 50
print("Removing elements larger than 50 from the list:\n", resultList)

輸出

執行後,以上程式將生成以下輸出 -

The Given list is: [45, 150, 20, 90, 15, 55, 12, 75]
Removing elements larger than 50 from the list:
 [45, 20, 15, 12]

結論

在本文中,我們學習了 4 種不同的 Python 方法來刪除大於給定值的列表元素。此外,我們還學習瞭如何使用 lambda 和 filter() 函式根據條件過濾列表。

更新於: 2023年1月31日

7K+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.