Python 删除大于特定值的列表元素
在本文中,我们将学习如何从Python的列表中删除大于特定值的元素。
使用的方法
以下是用于完成此任务的各种方法 –
- 使用remove()方法
-
使用列表推导
-
使用filter()方法和lambda函数
方法1:使用remove()方法
remove() 函数(从列表中删除第一个出现的元素)
步骤
以下是执行所需任务的算法/步骤。 –
- 创建一个变量来存储 输入列表 。
-
创建另一个变量来存储另一个 输入值 。
-
使用 for循环 遍历输入列表中的每个元素。
-
使用 if条件 语句,检查当前元素是否大于指定的输入值。
-
如果条件为真,请使用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()函数从输入列表中删除大于指定输入值的元素。
# 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()函数根据条件过滤列表。