在本文中,我们将学习如何从 Python 中的列表中删除大于特定值的元素。
以下是用于完成此任务的各种方法 -
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]
当您希望基于现有列表的值构建新列表时,列表推导提供了更短/更简洁的语法。
以下程序使用列表推导式从输入列表中删除大于指定输入值的元素 −
# 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]
Lambda 函数,通常称为“匿名函数”,与普通的 Python 函数相同,只是它可以在没有名称的情况下定义。def 关键字用于定义普通函数,而 lambda 关键字用于定义匿名函数。但是,它们仅限于单一的表达方式。与常规函数一样,它们可以接受多个参数。
lambda arguments: expression
以下是执行所需任务要遵循的算法/步骤。−
以下程序使用 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]
以下程序使用 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() 函数根据条件过滤列表。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有