卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章18144本站已运行340

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

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

'
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]
卓越飞翔博客
上一篇: C++程序以查找订阅OTT服务所需的最少金额
下一篇: 返回列表

相关推荐

留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏