0

0

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

PHPz

PHPz

发布时间:2023-09-07 23:33:10

|

763人浏览过

|

来源于tutorialspoint

转载

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

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

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

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

立即学习Python免费学习笔记(深入)”;

输入输出场景

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

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 列表中删除元素有多种方法。让我们一一讨论。

立即学习Python免费学习笔记(深入)”;

使用Remove()方法

立即学习Python免费学习笔记(深入)”;

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

示例

立即学习Python免费学习笔记(深入)”;

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

Moshi Chat
Moshi Chat

法国AI实验室Kyutai推出的端到端实时多模态AI语音模型,具备听、说、看的能力,不仅可以实时收听,还能进行自然对话。

下载
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.

示例

立即学习Python免费学习笔记(深入)”;

让我们看一个例子 -

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.

示例

立即学习Python免费学习笔记(深入)”;

在此示例中,我们使用 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速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

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

相关专题

更多
python开发工具
python开发工具

php中文网为大家提供各种python开发工具,好的开发工具,可帮助开发者攻克编程学习中的基础障碍,理解每一行源代码在程序执行时在计算机中的过程。php中文网还为大家带来python相关课程以及相关文章等内容,供大家免费下载使用。

716

2023.06.15

python打包成可执行文件
python打包成可执行文件

本专题为大家带来python打包成可执行文件相关的文章,大家可以免费的下载体验。

626

2023.07.20

python能做什么
python能做什么

python能做的有:可用于开发基于控制台的应用程序、多媒体部分开发、用于开发基于Web的应用程序、使用python处理数据、系统编程等等。本专题为大家提供python相关的各种文章、以及下载和课程。

739

2023.07.25

format在python中的用法
format在python中的用法

Python中的format是一种字符串格式化方法,用于将变量或值插入到字符串中的占位符位置。通过format方法,我们可以动态地构建字符串,使其包含不同值。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

617

2023.07.31

python教程
python教程

Python已成为一门网红语言,即使是在非编程开发者当中,也掀起了一股学习的热潮。本专题为大家带来python教程的相关文章,大家可以免费体验学习。

1236

2023.08.03

python环境变量的配置
python环境变量的配置

Python是一种流行的编程语言,被广泛用于软件开发、数据分析和科学计算等领域。在安装Python之后,我们需要配置环境变量,以便在任何位置都能够访问Python的可执行文件。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

547

2023.08.04

python eval
python eval

eval函数是Python中一个非常强大的函数,它可以将字符串作为Python代码进行执行,实现动态编程的效果。然而,由于其潜在的安全风险和性能问题,需要谨慎使用。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

575

2023.08.04

scratch和python区别
scratch和python区别

scratch和python的区别:1、scratch是一种专为初学者设计的图形化编程语言,python是一种文本编程语言;2、scratch使用的是基于积木的编程语法,python采用更加传统的文本编程语法等等。本专题为大家提供scratch和python相关的文章、下载、课程内容,供大家免费下载体验。

699

2023.08.11

php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

热门下载

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

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号