陣列是儲存在連續記憶體位置的同類資料類型元素的集合。 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中文網其他相關文章!

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Python3.6環境下加載Pickle文件報錯:ModuleNotFoundError:Nomodulenamed...

如何解決jieba分詞在景區評論分析中的問題?當我們在進行景區評論分析時,往往會使用jieba分詞工具來處理文�...

如何使用正則表達式匹配到第一個閉合標籤就停止?在處理HTML或其他標記語言時,常常需要使用正則表達式來�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)