如何在 Python 中比較兩個列表

Python 提供了多種方法來比較這兩個列表。比較是將的數據項與列表中的另一個數據項進行檢查的過程,無論它們是否相同。


list1 - [11, 12, 13, 14, 15]
list2 - [11, 12, 13, 14, 15]
Output - The lists are equal

下面給出了比較兩個列表的方法。

  • cmp()函數
  • set()函數和==運算符
  • sort()函數和==運算符
  • collection.counter()函數
  • reduce()和 map()函數

cmp()函數

Python cmp()函數比較兩個 Python 對象,根據比較結果返回整數值-1,0,1。

注意——它在 Python 3.x 版本中不使用。

set()函數和==運算符

Python set() 函數操縱列表進入集合而不考慮元素的順序。此外,我們使用等於運算符(==)來比較列表的數據項。讓我們理解下面的例子。

示例-


list1 = [11, 12, 13, 14, 15]
list2 = [12, 13, 11, 15, 14]

a = set(list1)
b = set(list2)

if a == b:
    print("The list1 and list2 are equal")
else:
    print("The list1 and list2 are not equal")

輸出:

The list1 and list2 are equal

解釋:

在上面的例子中,我們已經聲明了要相互比較的兩個列表。我們將這些列錶轉換成集合,並在==運算符的幫助下比較每個元素。兩個列表中的所有元素都是相等的,那麼如果執行了 block 並列印了結果。

帶有==運算符的 sort()方法

Python sort() 函數用於排序列表。同一個列表的元素是指同一個索引位置;列表是平等的。

注意——在 sort()方法中,我們可以以任何順序傳遞列表項,因為我們是在比較之前排序列表。

讓我們理解下面的例子-

示例-


import collections

list1 = [10, 20, 30, 40, 50, 60]
list2 = [10, 20, 30, 50, 40, 70]
list3 = [50, 10, 30, 20, 60, 40]

# Sorting the list
list1.sort()
list2.sort()
list3.sort()

if list1 == list2:
    print("The list1 and list2 are the same")
else:
    print("The list1 and list3 are not the same")

if list1 == list3:
    print("The list1 and list2 are not the same")
else:
    print("The list1 and list2 are not the same")

輸出:

The list1 and list3 are not the same
The list1 and list2 are not the same

collection.counter()函數

collections模塊提供計數器(),,有效比較列表。它以字典格式<值> : <頻率>存儲數據,並計算列表項目的頻率。

注意——列表元素的順序在這個函數中並不重要。

示例-


import collections

list1 = [10, 20, 30, 40, 50, 60]
list2 = [10, 20, 30, 50, 40, 70]
list3 = [50, 10, 30, 20, 60, 40]

if collections.Counter(list1) == collections.Counter(list2):
    print("The lists l1 and l2 are the same")
else:
    print("The lists l1 and l2 are not the same")

if collections.Counter(list1) == collections.Counter(list3):
    print("The lists l1 and l3 are the same")
else:
    print("The lists l1 and l3 are not the same")

輸出:

The lists list1 and list2 are not the same
The lists list1 and list3 are the same

reduce()和 map()

map() 函數接受一個函數和 Python 可迭代對象(列表、元組、字元串等)作為參數,並返回一個 map 對象。該函數對列表的每個元素實現,並返回一個迭代器作為結果。

此外, reduce() 方法對可迭代對象遞歸實現給定的函數。

這裡,我們將結合使用這兩種方法。 map() 函數將函數(可以是用戶定義的函數或 lambda 函數)實現到每個可迭代對象,而 reduce() 函數負責以遞歸方式應用。

注意-我們需要導入 functool 模塊來使用 reduce()函數。

讓我們理解下面的例子。

示例-


import functools

list1 = [10, 20, 30, 40, 50]
list2 = [10, 20, 30, 50, 40, 60, 70]
list3 = [10, 20, 30, 40, 50]

if functools.reduce(lambda x, y: x and y, map(lambda a, b: a == b, list1, list2), True):
    print("The list1 and list2 are the same")
else:
    print("The list1 and list2 are not the same")

if functools.reduce(lambda x, y: x and y, map(lambda a, b: a == b, list1, list3), True):
    print("The list1 and list3 are the same")
else:
    print("The list1 and list3 are not the same")

輸出:

The list1 and list2 are not the same
The list1 and list3 are the same

在本節中,我們已經介紹了在 Python 中比較兩個列表的各種方法。


原創文章,作者:MZXU9,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/130532.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
MZXU9的頭像MZXU9
上一篇 2024-10-03 23:28
下一篇 2024-10-03 23:28

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • 如何在PyCharm中安裝OpenCV?

    本文將從以下幾個方面詳細介紹如何在PyCharm中安裝OpenCV。 一、安裝Python 在安裝OpenCV之前,請確保已經安裝了Python。 如果您還沒有安裝Python,可…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智慧、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29

發表回復

登錄後才能評論