当前位置:首页 > 编程技术 > 正文内容

使用 Python 删除大于特定值的列表元素

yc8881年前 (2023-02-22)编程技术675

使用 Python 删除大于特定值的列表元素

在本文中,我们将学习如何从 Python 中的列表中删除大于特定值的元素。

使用的方法

以下是用于完成此任务的各种方法 -

  • 使用 remove() 方法

  • 使用列表理解

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

方法 1:使用 remove() 方法

remove() 函数(从列表中删除元素的第一次出现)

算法(步骤)

以下是执行所需任务要遵循的算法/步骤。−

  • 创建一个变量来存储输入列表

  • 创建另一个变量来存储另一个输入值

  • 使用 for 循环循环访问输入列表中的每个元素。

  • 使用 if 条件语句检查当前元素是否大于指定的输入值。

  • 如果条件为 true,则使用 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 函数,通常称为“匿名函数”,与普通的 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() 函数根据条件过滤列表。


本站发布的内容若侵犯到您的权益,请邮件联系站长删除,我们将及时处理!


从您进入本站开始,已表示您已同意接受本站【免责声明】中的一切条款!


本站大部分下载资源收集于网络,不保证其完整性以及安全性,请下载后自行研究。


本站资源仅供学习和交流使用,版权归原作者所有,请勿商业运营、违法使用和传播!请在下载后24小时之内自觉删除。


若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,使用者自行承担,概与本站无关。


本文链接:https://www.10zhan.com/biancheng/10586.html

标签: Python
分享给朋友:

“使用 Python 删除大于特定值的列表元素” 的相关文章

【说站】Thymeleaf报错Error resolving template “XXX”

【说站】Thymeleaf报错Error resolving template “XXX”

修改了一下开源项目的目录结构访问突然报错Error resolving template “XXX”可能原因有如下三种:第一种可能:原因:在使用springboot的过程中,如果使用thymeleaf...

【说站】用一句话就可以去除宝塔面板操作上的二次验证

【说站】用一句话就可以去除宝塔面板操作上的二次验证

用过宝塔的朋友应该都会发现,现在宝塔面板有些鸡肋的功能,删除文件、删除数据库、删除站点等操作都需要做计算题!不仅加了几秒的延时等待,还无法跳过!这时候就会有朋友在想,如何去除宝塔面板的二次验证,此篇文...

【说站】Centos8.0如何配置静态IP详解及永久关闭防火墙

【说站】Centos8.0如何配置静态IP详解及永久关闭防火墙

这篇文章主要介绍了详解Centos8 配置静态IP的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来学习一下!1. 查看自己的网关地址点击虚...

【说站】利用Webhook实现Java项目自动化部署

【说站】利用Webhook实现Java项目自动化部署

用webhook就能实现Java项目自动部署,其实原理很简单。费话不多说,直接往下看教程。1. 创建gitee仓库并初始化2. 在linux安装git3. 在宝塔的软件的商店里下载Webhook4....

【说站】vagrant实现linux虚拟机的安装并配置网络

【说站】vagrant实现linux虚拟机的安装并配置网络

一、VirtualBox的下载和安装1、下载VirtualBox官网下载:https://www.virtualbox.org/wiki/Downloads我的电脑是Windows的,所以下载Wind...

【说站】C#在PDF中添加墨迹注释Ink Annotation的步骤详解

【说站】C#在PDF中添加墨迹注释Ink Annotation的步骤详解

PDF中的墨迹注释(Ink Annotation),表现为徒手涂鸦式的形状;该类型的注释,可任意指定形状顶点的位置及个数,通过指定的顶点,程序将连接各点绘制成平滑的曲线。下面,通过C#程序代码介绍如何...