国产睡熟迷奷白丝护士系列精品,中文色字幕网站,免费h网站在线观看的,亚洲开心激情在线

      <sup id="hb9fh"></sup>
          1. 千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機(jī)構(gòu)

            手機(jī)站
            千鋒教育

            千鋒學(xué)習(xí)站 | 隨時(shí)隨地免費(fèi)學(xué)

            千鋒教育

            掃一掃進(jìn)入千鋒手機(jī)站

            領(lǐng)取全套視頻
            千鋒教育

            關(guān)注千鋒學(xué)習(xí)站小程序
            隨時(shí)隨地免費(fèi)學(xué)習(xí)課程

            當(dāng)前位置:首頁  >  技術(shù)干貨  > 如何在Python中比較兩個(gè)列表

            如何在Python中比較兩個(gè)列表

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

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

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

            下面給出了比較兩個(gè)列表的方法。

              cmp()函數(shù)

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

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

              collection.counter()函數(shù)

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

            cmp()函數(shù)

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

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

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

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

            示例-

            
            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)聲明了要相互比較的兩個(gè)列表。我們將這些列表轉(zhuǎn)換成集合,并在==運(yùn)算符的幫助下比較每個(gè)元素。兩個(gè)列表中的所有元素都是相等的,那么如果執(zhí)行了 block 并打印了結(jié)果。

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

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

            注意——在 sort()方法中,我們可以以任何順序傳遞列表項(xiàng),因?yàn)槲覀兪窃诒容^之前排序列表。

            讓我們理解下面的例子-

            示例-

            
            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模塊提供計(jì)數(shù)器(),,有效比較列表。它以字典格式<值> : <頻率>存儲(chǔ)數(shù)據(jù),并計(jì)算列表項(xiàng)目的頻率。

            注意——列表元素的順序在這個(gè)函數(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ù)接受一個(gè)函數(shù)和 Python 可迭代對(duì)象(列表、元組、字符串等)作為參數(shù),并返回一個(gè) map 對(duì)象。該函數(shù)對(duì)列表的每個(gè)元素實(shí)現(xiàn),并返回一個(gè)迭代器作為結(jié)果。

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

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

            注意-我們需要導(dǎo)入 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 中比較兩個(gè)列表的各種方法。

            tags: python教程
            聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
            10年以上業(yè)內(nèi)強(qiáng)師集結(jié),手把手帶你蛻變精英
            請(qǐng)您保持通訊暢通,專屬學(xué)習(xí)老師24小時(shí)內(nèi)將與您1V1溝通
            免費(fèi)領(lǐng)取
            今日已有369人領(lǐng)取成功
            劉同學(xué) 138****2860 剛剛成功領(lǐng)取
            王同學(xué) 131****2015 剛剛成功領(lǐng)取
            張同學(xué) 133****4652 剛剛成功領(lǐng)取
            李同學(xué) 135****8607 剛剛成功領(lǐng)取
            楊同學(xué) 132****5667 剛剛成功領(lǐng)取
            岳同學(xué) 134****6652 剛剛成功領(lǐng)取
            梁同學(xué) 157****2950 剛剛成功領(lǐng)取
            劉同學(xué) 189****1015 剛剛成功領(lǐng)取
            張同學(xué) 155****4678 剛剛成功領(lǐng)取
            鄒同學(xué) 139****2907 剛剛成功領(lǐng)取
            董同學(xué) 138****2867 剛剛成功領(lǐng)取
            周同學(xué) 136****3602 剛剛成功領(lǐng)取
            相關(guān)推薦HOT
            為什么Hadoop是用Java實(shí)現(xiàn)的?

            一、跨平臺(tái)能力多平臺(tái)運(yùn)行:Java的“一次編寫,到處運(yùn)行”理念,使得Hadoop能在各種操作系統(tǒng)和硬件上運(yùn)行,不需要特定的調(diào)整。廣泛應(yīng)用:這一特...詳情>>

            2023-10-15 16:51:37
            ECU是什么?

            1、ECU的基本定義與作用ECU,全稱為電子控制單元,是一種專門用于控制汽車各個(gè)系統(tǒng)的微處理器控制系統(tǒng)。通過接收傳感器的信號(hào)并轉(zhuǎn)換成控制指令...詳情>>

            2023-10-15 16:29:54
            什么是SOA?

            1、SOA的基本概念與核心原則SOA是一種使軟件組件通過網(wǎng)絡(luò)進(jìn)行互操作的架構(gòu)模式。核心原則包括:可發(fā)現(xiàn)的服務(wù):服務(wù)應(yīng)容易發(fā)現(xiàn)和理解。松耦合:...詳情>>

            2023-10-15 16:19:32
            什么是內(nèi)存池?

            1、內(nèi)存池的基本概念內(nèi)存池是一種內(nèi)存管理策略,旨在優(yōu)化內(nèi)存分配性能和減少碎片化。通過將內(nèi)存分配到大小固定的池中,應(yīng)用程序可以快速、高效...詳情>>

            2023-10-15 16:16:15
            ci構(gòu)建與編譯的區(qū)別是什么?

            一、功能與目的構(gòu)建(Build): 構(gòu)建是將源代碼轉(zhuǎn)化為可執(zhí)行代碼的過程,它包括編譯、鏈接、打包等一系列步驟。構(gòu)建不僅僅局限于編譯,還可能涉...詳情>>

            2023-10-15 15:57:11
            快速通道