Python 是一種廣泛使用的軟體,它有許多不同的使用目的和執行不同任務的多種功能。 python 的一個有用的功能是列表功能,它有助於收集和儲存不同的數據,但很多時候用戶在刪除另一個子列表中已經存在的子列表時會遇到問題。因此,在本文中,我們將學習如何刪除其他子清單中已存在的不同子清單。
為了清楚地理解這個問題,我們舉一個例子,我們必須刪除資料已經存在於另一個子清單中的子清單。
範例
duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]] #All the sublist whose data is already present in other sublist are to be removed
輸出
名稱為 [Shyam,John] 和 [David,Stefan] 的子列表已在其他子列表中具有相同的數據,因此這些額外的子列表將被刪除。輸出應如下圖所示:
new_list = [[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
現在我們將了解刪除子清單中已存在的子清單的不同方法。
這裡我們提到了不同的可能方法:
列表理解
刪除其他子清單中存在的所有子清單的最簡單方法是藉助清單理解。檢查列表中存在的所有子列表,並將那些不存在於任何其他子列表中的子列表複製到新列表中。我們舉個例子來更清楚地理解:
範例
duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]] New_list = [sublist for sublist in duplicate_list if not any(set(sublist) <= set(other) for other in duplicate_list if sublist is not other)] #We first check all the lists of the duplicate list through the any() function and then we check for any repeatation with the help of <= operator
輸出
程式碼完成後,我們將列印上述程式碼的輸出。上述程式碼的輸出如下:
[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
所有額外的子清單都被刪除,因此我們編寫了正確的程式碼來刪除子清單中已經存在的子清單。
定義函數
解決該問題的另一種方法是建立一個全新的單獨函數來過濾掉其他子清單中存在的所有子清單。這可以透過為函數定義一個條件並使其相應地運行來完成。
範例
def is_sublist(sublist, other): #is_sublist is the function defined return set(sublist) <= set(other) #the functions checks 2 sublists at a time and if the sublist already exists then it returns with `true` feedback and does not consider the extra sublist duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]] new_list = [sublist for sublist in duplicate_list if not any(is_sublist(sublist, other) for other in duplicate_list if sublist is not other)]
輸出
上述程式碼的輸出如下:
[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
所有額外的子清單都被刪除,因此我們編寫了正確的程式碼來刪除所有額外的子清單。
比較每個列表
這是一種非常複雜的方法,用於刪除已存在於另一個子清單中的子清單。在此方法中,所有子清單都會相互比較,並將不重複的子清單複製到新清單中。我們可以藉助以下範例來理解這一點:
範例
duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]] #A copy of duplicate list is created to avoid any errors in the original file new_list = duplicate_list[:] #Check each sublist present in the new_list for sublist in duplicate_list: for other in new_list: # Checking of presence of sublist present in other sublist is done if sublist != other and set(sublist).issubset(other): #issubset is used to check presence of one sublist in another sublist # Remove all the repeating sublist new_list.remove(sublist) break #break is used to stop the loop so that it does not keep checking continuosly print(new_list)
輸出
上述程式碼的輸出如下:
[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
當清單太長並且包含大量元素較多的子清單時,此方法較為合適。
設定操作
在此操作中,在設定操作的幫助下刪除其他子清單中存在的子清單。在這種方法中,我們可以將清單中的每個子清單轉換為集合,並且借助不同的操作,我們可以刪除其他子清單中存在的所有子清單。我們透過下面的例子可以更清楚地理解:
範例
duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]] new_list = [] for sublist in duplicate_list: is_subset = False for other in duplicate_list: if sublist != other and set(sublist).difference(set(other)) == set(): #The difference operation is used to calculate the difference betwen two sets is_subset = True #When the sublist is present in another sublist the result of is_subset will be true break #Once the result is found to be true, the loop is broke and all the other sublist are copied into the new_list if not is_subset: new_list.append(sublist) print(new_list)
輸出
上述程式碼的輸出如下:
[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
其他子清單中存在的所有子清單均已刪除。
結論
刪除另一個子清單中已經存在的子清單的問題是使用者經常面臨的問題,很多時候它會導致消耗使用者大量的時間。因此,可以使用上一篇文章中建議的不同方法快速刪除另一個子清單中存在的所有子清單。
以上是Python - 刪除在另一個子列表中存在的子列表的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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或其他標記語言時,常常需要使用正則表達式來�...

攻克Investing.com的反爬蟲策略許多人嘗試爬取Investing.com(https://cn.investing.com/news/latest-news)的新聞數據時,常常�...


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

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

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3漢化版
中文版,非常好用