在 Python 中獲取兩個列表之間的差異
- 
          
            在 Python 中使用 set.difference()查詢兩個列表之間的差異
- 
          
            在 Python 中使用 set.symmetric_difference()查詢兩個列表之間的差異
- 
          
            在 Python 中使用關鍵字 in查詢兩個列表之間的差異
- 
          
            在 Python 中使用 NumPy庫查詢兩個列表之間的差異
 
有許多對資料結構執行的操作,如列表、字典、元組和陣列。在 Python 中,這些資料結構幾乎用在每個程式中。這些資料結構之一是 Python 中的列表。Python 程式設計中的列表有助於在一個變數中儲存多個專案。找出兩個列表之間的差異是幾乎每個程式設計師都會執行的一項非常常見的任務。
本教程將演示幾種在 Python 中獲取兩個列表之間差異的方法。
在 Python 中使用 set.difference() 查詢兩個列表之間的差異
set() 方法幫助使用者將任何可迭代物件轉換為可迭代序列,也稱為集合。可迭代物件可以是列表、字典或元組。
set.difference() 函式用於返回兩個集合之間的差異。這個功能基本上消除了兩組中的共同元素。
list_1 = [5, 10, 15, 20, 25, 30]
list_2 = [10, 20, 30, 40, 50, 60]
difference_1 = set(list_1).difference(set(list_2))
difference_2 = set(list_2).difference(set(list_1))
list_difference = list(difference_1.union(difference_2))
print(list_difference)
輸出:
[50, 5, 40, 25, 60, 15]
對於這種方法,第一步是將兩個列表儲存在兩個變數中。set() 函式用於將這些列表轉換為集合。在同一步驟中,set.difference() 函式用於查詢兩個集合之間的差異。請注意,difference_1 變數獲取 list_1 中的元素,而不是 list_2 中的元素。
difference_2 變數獲取在 list_2 中而不是在 list_1 中的元素。之後,union 函式用於從 difference_1 和 difference_2 變數中獲取所有元素。此外,list() 函式用於再次將兩個集合轉換為列表。最後,列印結果列表。
在 Python 中使用 set.symmetric_difference() 查詢兩個列表之間的差異
    
在這裡,set() 方法最初用於將兩個列表轉換為集合。
symmetric_difference() 方法用於返回第一組或第二組中的元素。此方法不會像兩個集合的公共元素一樣返回交集。
list_1 = [5, 10, 15, 20, 25, 30]
list_2 = [10, 20, 30, 40, 50, 60]
set_difference = set(list_1).symmetric_difference(set(list_2))
list_difference = list(set_difference)
print(list_difference)
輸出:
[5, 40, 15, 50, 25, 60]
set() 方法也用於將列表轉換為集合。得到差值後,list() 函式用於獲取和轉換最終集合,就像將 set_difference 轉換回列表一樣。
在 Python 中使用關鍵字 in 查詢兩個列表之間的差異
in 關鍵字有助於檢查元素是否存在於可迭代序列(如列表)中。
append() 函式也用於此方法。append() 用於向現有列表新增元素;它不會建立新列表,而是通過向其中新增元素來修改初始列表。
list_1 = [5, 10, 15, 20, 25, 30]
list_2 = [10, 20, 30, 40, 50, 60]
list_difference = []
for element in list_1:
    if element not in list_2:
        list_difference.append(element)
print(list_difference)
輸出:
[5, 15, 25]
在此方法中,我們首先初始化必須用零元素列印的最終列表。然後我們使用 for 迴圈從第一個列表開始迭代,即 list_1。之後,我們使用關鍵字 not in 來檢查元素是否在第二個列表中。
最後,我們使用 append() 方法將不在第二個列表中的元素新增到初始列表,即 list_difference。但是,此方法的缺點是它不返回第二個列表中的元素。
為了使這個方法更簡單,使用列表推導式。列表推導式是一種幫助使用現有列表的元素建立新列表的方法。
list_1 = [5, 10, 15, 20, 25, 30]
list_2 = [10, 20, 30, 40, 50, 60]
list_difference = [element for element in list_1 if element not in list_2]
print(list_difference)
輸出:
[5, 15, 25]
在 Python 中使用 NumPy 庫查詢兩個列表之間的差異
Python 的 NumPy 庫是一個非常常用的庫。這個庫幫助使用者在陣列、矩陣和線性代數中執行任務。
在此方法中,使用 NumPy 庫的三個函式來獲取兩個列表之間的差異。這三個函式是 np.array()、np.setdiff1d() 和 np.concatenate()。
陣列是 NumPy 庫的主要資料結構。np.array() 函式可幫助你建立陣列並協助你處理它們。
np.setdiff1d() 函式可幫助你找到兩個陣列之間的差集,並返回第一個陣列中而不是第二個陣列中的值。
np.concatenate 函式用於將兩個或多個 NumPy 陣列組合在一起。參考下面的例子:
import numpy as np
list_1 = [5, 10, 15, 20, 25, 30]
list_2 = [10, 20, 30, 40, 50, 60]
array_1 = np.array(list_1)
array_2 = np.array(list_2)
difference_1 = np.setdiff1d(array_1, array_2)
difference_2 = np.setdiff1d(array_2, array_1)
list_difference = np.concatenate((difference_1, difference_2))
print(list(list_difference))
輸出:
[5, 15, 25, 40, 50, 60]
Lakshay Kapoor is a final year B.Tech Computer Science student at Amity University Noida. He is familiar with programming languages and their real-world applications (Python/R/C++). Deeply interested in the area of Data Sciences and Machine Learning.
LinkedIn