目錄
範例
輸出
列表理解
定義函數
比較每個列表
設定操作
結論
首頁 後端開發 Python教學 Python - 刪除在另一個子列表中存在的子列表

Python - 刪除在另一個子列表中存在的子列表

Sep 18, 2023 pm 06:53 PM

Python - 删除在另一个子列表中存在的子列表

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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

如何解決Linux終端中查看Python版本時遇到的權限問題? 如何解決Linux終端中查看Python版本時遇到的權限問題? Apr 01, 2025 pm 05:09 PM

Linux終端中查看Python版本時遇到權限問題的解決方法當你在Linux終端中嘗試查看Python的版本時,輸入python...

如何在使用 Fiddler Everywhere 進行中間人讀取時避免被瀏覽器檢測到? 如何在使用 Fiddler Everywhere 進行中間人讀取時避免被瀏覽器檢測到? Apr 02, 2025 am 07:15 AM

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

在Python中如何高效地將一個DataFrame的整列複製到另一個結構不同的DataFrame中? 在Python中如何高效地將一個DataFrame的整列複製到另一個結構不同的DataFrame中? Apr 01, 2025 pm 11:15 PM

在使用Python的pandas庫時,如何在兩個結構不同的DataFrame之間進行整列複製是一個常見的問題。假設我們有兩個Dat...

Uvicorn是如何在沒有serve_forever()的情況下持續監聽HTTP請求的? Uvicorn是如何在沒有serve_forever()的情況下持續監聽HTTP請求的? Apr 01, 2025 pm 10:51 PM

Uvicorn是如何持續監聽HTTP請求的? Uvicorn是一個基於ASGI的輕量級Web服務器,其核心功能之一便是監聽HTTP請求並進�...

如何在10小時內通過項目和問題驅動的方式教計算機小白編程基礎? 如何在10小時內通過項目和問題驅動的方式教計算機小白編程基礎? Apr 02, 2025 am 07:18 AM

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

在Linux終端中使用python --version命令時如何解決權限問題? 在Linux終端中使用python --version命令時如何解決權限問題? Apr 02, 2025 am 06:36 AM

Linux終端中使用python...

如何繞過Investing.com的反爬蟲機制獲取新聞數據? 如何繞過Investing.com的反爬蟲機制獲取新聞數據? Apr 02, 2025 am 07:03 AM

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

See all articles