久久精品国产亚洲高清|精品日韩中文乱码在线|亚洲va中文字幕无码久|伊人久久综合狼伊人久久|亚洲不卡av不卡一区二区|精品久久久久久久蜜臀AV|国产精品19久久久久久不卡|国产男女猛烈视频在线观看麻豆

    1. <style id="76ofp"></style>

      <style id="76ofp"></style>
      <rt id="76ofp"></rt>
      <form id="76ofp"><optgroup id="76ofp"></optgroup></form>
      1. 千鋒教育-做有情懷、有良心、有品質的職業(yè)教育機構

        手機站
        千鋒教育

        千鋒學習站 | 隨時隨地免費學

        千鋒教育

        掃一掃進入千鋒手機站

        領取全套視頻
        千鋒教育

        關注千鋒學習站小程序
        隨時隨地免費學習課程

        當前位置:首頁  >  技術干貨  > 如何在Python中比較兩個列表

        如何在Python中比較兩個列表

        來源:千鋒教育
        發(fā)布人:xqq
        時間: 2023-07-21 17:01:08 1689930068

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

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

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

          cmp()函數(shù)

          set()函數(shù)和==運算符

          sort()函數(shù)和==運算符

          collection.counter()函數(shù)

          reduce()和 map()函數(shù)

        cmp()函數(shù)

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

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

        set()函數(shù)和==運算符

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

        示例-

        
        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
        

        解釋:

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

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

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

        注意——在 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()函數(shù)

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

        注意——列表元素的順序在這個函數(shù)中并不重要。

        示例-

        
        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() 函數(shù)接受一個函數(shù)和 Python 可迭代對象(列表、元組、字符串等)作為參數(shù),并返回一個 map 對象。該函數(shù)對列表的每個元素實現(xiàn),并返回一個迭代器作為結果。

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

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

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

        讓我們理解下面的例子。

        示例-

        
        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
        

        在本節(jié)中,我們已經(jīng)介紹了在 Python 中比較兩個列表的各種方法。

        tags: python教程
        聲明:本站稿件版權均屬千鋒教育所有,未經(jīng)許可不得擅自轉載。
        10年以上業(yè)內強師集結,手把手帶你蛻變精英
        請您保持通訊暢通,專屬學習老師24小時內將與您1V1溝通
        免費領取
        今日已有369人領取成功
        劉同學 138****2860 剛剛成功領取
        王同學 131****2015 剛剛成功領取
        張同學 133****4652 剛剛成功領取
        李同學 135****8607 剛剛成功領取
        楊同學 132****5667 剛剛成功領取
        岳同學 134****6652 剛剛成功領取
        梁同學 157****2950 剛剛成功領取
        劉同學 189****1015 剛剛成功領取
        張同學 155****4678 剛剛成功領取
        鄒同學 139****2907 剛剛成功領取
        董同學 138****2867 剛剛成功領取
        周同學 136****3602 剛剛成功領取
        相關推薦HOT
        怀柔区| 恩平市| 静乐县| 永福县| 比如县| 庆元县| 竹山县| 定襄县| 沙河市| 宜城市| 岑溪市| 黔西县| 怀来县| 许昌县| 大化| 高碑店市| 温宿县| 南通市| 双辽市| 文山县| 永春县| 花莲县| 特克斯县| 麟游县| 武夷山市| 咸阳市| 陆川县| 福贡县| 延庆县| 秀山| 沽源县| 文昌市| 高平市| 山丹县| 万载县| 资兴市| 桂东县| 威信县| 大石桥市| 武川县| 铜山县|