总结
豆包 AI 助手文章总结

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

PHPz
发布: 2023-09-07 23:33:10
转载
694人浏览过

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 方法

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程序:删除数组/列表中的所有元素的出现次数的详细内容,更多请关注php中文网其它相关文章!

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

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

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

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