Python程序:删除数组/列表中的所有元素的出现次数

PHPz

PHPz

2023-09-07

763人浏览

转载

python程序:删除数组/列表中的所有元素的出现次数

数组是存储在连续内存位置的同类数据类型元素的集合。 Python 不提供对内置数组的支持。如果您需要使用数组,则需要导入“array”模块,或者使用 numpy 库中的数组。

我们可以在 Python 中使用列表而不是数组。但是,我们不能限制列表的元素具有相同的数据类型。

给定的任务是删除数组/列表中所有出现的元素。 IE。我们删除指定的元素,包括重复的元素。让我们通过考虑输入输出场景来了解其实际工作原理。

输入输出场景

考虑一个由一个或多个重复元素(重复元素)组成的列表。

my_list = [ 1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10 ].

现在,假设我们需要删除元素 10。我们可以清楚地看到该元素10 出现在列表中并且重复 5 次。删除所有出现的内容后结果列表将如下 -

my_list = [ 1, 20, 21, 16, 18, 22, 8 ].

从 Python 列表中删除元素有多种方法。让我们一一讨论。

使用Remove()方法

Python 中的remove() 方法接受单个值,表示列表中的元素作为参数,并将其从当前列表中删除。为了使用此方法删除所有出现的元素,我们需要将所需元素与列表中的所有其他元素进行比较,并且每当发生匹配时,我们需要调用remove()方法。

示例

在此示例中,我们将创建一个元素列表,并使用remove()删除所有出现的值 10 方法

Flowstep
Flowstep

AI界面设计工具,通过对话几秒内创建UI设计图、线框图和流程图

下载
def removing_elements(my_list, element):
   element_count = my_list.count(element)
   for i in range(element_count):
      my_list.remove(element)
   return my_list
if __name__ == "__main__":
   my_list = [1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10]
   element = 10
   print("The list before performing the removal operation is: ")
   print(my_list)
   result = removing_elements(my_list, element)
   print("The list after performing the removal operation is: ")
   print(result)

输出

上述程序的输出如下 -

The list before performing the removal operation is: 
[1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10]
The list after performing the removal operation is:
[1, 20, 21, 16, 18, 22, 8]

使用列表理解

The technique “List Comprehension” consists of lengthy one-line statements that can perform the entire task. Using this a new can be constructed such that the rest of the elements can be stored whenever the given base condition is satisfied. 

Here, we search for the desired element and after it is found, we constructed another list such that the matched elements are excluded ie. Except for the matched elements, all other elements will be stored within the newly constructed list which is finally considered as the resulting list.

示例

让我们看一个例子 -

def removing_elements(my_list, element):
   result = [i for i in my_list if i != element]
   return result

if __name__ == "__main__":
   my_list = [1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10]
   element = 10
   print("The list before performing the removal operation is: ")
   print(my_list)
   result = removing_elements(my_list, element)
   print("The list after performing the removal operation is: ")
   print(result) 

输出

上述程序的输出如下 -

The list before performing the removal operation is: 
[1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10]
The list after performing the removal operation is:
[1, 20, 21, 16, 18, 22, 8]

使用“Filter()”方法

The method filter() accepts a function and an iterable object as parameters and filters the elements of the given iterable based on the condition described by the function.

Here, using the filter() and __ne__ (functionality of the not equal operator) methods we can filter the elements of a list that are not equal to the desired element.

示例

在此示例中,我们使用 filter() 方法删除列表中特定元素的所有出现。

def removing_elements(my_list, element):
   result = list(filter((element).__ne__, my_list))
   return result

if __name__ == "__main__":
   my_list = [1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10] 
   element = 10
   print("The list before performing the removal operation is: ")
   print(my_list)
   result = removing_elements(my_list, element)

   print("The list after performing the removal operation is: ")
   print(result)

输出

上述程序的输出如下 -

The list before performing the removal operation is: 
[1, 10, 20, 10, 21, 16, 18, 10, 22, 10, 8, 10]
The list after performing the removal operation is:
[1, 20, 21, 16, 18, 22, 8]

Python免费学习笔记(深入):立即使用
在学习笔记中,你将探索 Python 的核心概念和高级技巧!

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
数据类型有哪几种
数据类型有哪几种

数据类型有整型、浮点型、字符型、字符串型、布尔型、数组、结构体和枚举等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

2023.10.31

1259

5

php数据类型
php数据类型

本专题整合了php数据类型相关内容,阅读专题下面的文章了解更多详细内容。

2025.10.31

388

9

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

2026.02.12

298

19

function是什么
function是什么

function是函数的意思,是一段具有特定功能的可重复使用的代码块,是程序的基本组成单元之一,可以接受输入参数,执行特定的操作,并返回结果。本专题为大家提供function是什么的相关的文章、下载、课程内容,供大家免费下载体验。

2023.08.04

1387

5

js函数function用法
js函数function用法

js函数function用法有:1、声明函数;2、调用函数;3、函数参数;4、函数返回值;5、匿名函数;6、函数作为参数;7、函数作用域;8、递归函数。本专题提供js函数function用法的相关文章内容,大家可以免费阅读。

2023.10.07

253

5

墨刀AI提示词教学
墨刀AI提示词教学

本合集由PHP中文网精心整理,为您提供全面的墨刀AI提示词教学。内容涵盖高质量原型撰写公式与实操窍门,助您轻松掌握AI设计工具。无论是零基础入门还是进阶技巧,都能让您快速上手,大幅提升产品设计与协作效率。

2026.08.04

10

21

墨刀AI完整入门
墨刀AI完整入门

PHP中文网为您倾力打造墨刀AI保姆级入门指南完整版!本合集从零基础讲起,涵盖AI生成原型、提示词优化、图片转原型及多轮对话等核心功能。无论您是新手还是进阶用户,都能轻松掌握产品设计全流程。快来PHP中文网,一键解锁高效设计技巧,让想法即刻成型!

2026.08.04

8

20

墨刀AI进阶技巧
墨刀AI进阶技巧

本合集由PHP中文网精心整理,为您提供墨刀AI核心进阶策略指南。内容涵盖高效提示词写作、原型智能生成与微调、结构化导图制作及行业分析报告输出等实战技巧。助您轻松掌握AI设计工具,大幅提升产品设计与团队协作效率。

2026.08.04

10

14

火山引擎实名认证失败怎么办
火山引擎实名认证失败怎么办

火山引擎实名认证失败可能与证件信息填写错误、姓名或企业信息不一致、证件照片不清晰、营业执照状态异常、手机号验证失败或审核资料不完整有关。本专题整理个人认证、企业认证、资料上传、审核退回、重新提交和认证不通过的常见处理方法。

2026.08.04

5

10

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
热门推荐
/
最新课程
phpStudy极速入门视频教程
phpStudy极速入门视频教程

共6课时 | 54.4万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时 | 131.8万人学习