首頁  >  文章  >  後端開發  >  Python程式:刪除陣列/清單中的所有元素的出現次數

Python程式:刪除陣列/清單中的所有元素的出現次數

PHPz
PHPz轉載
2023-09-07 23:33:10629瀏覽

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 iefor. Except#ed  elements, all other elements will be stored within the newly constructed list which is finally considered as the resulting list.

#####1範例########## ###讓我們來看一個例子 -###
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.####ne__#Here, using the ter() 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程式:刪除陣列/清單中的所有元素的出現次數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除