欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中對(duì)列表排序?qū)嵗?/h1>
 更新時(shí)間:2015年01月04日 09:01:50   投稿:junjie  
這篇文章主要介紹了Python中對(duì)列表排序?qū)嵗?本文給出了9個(gè)List的排序?qū)嵗?需要的朋友可以參考下

很多時(shí)候,我們需要對(duì)List進(jìn)行排序,Python提供了兩個(gè)方法,對(duì)給定的List L進(jìn)行排序:

方法1.用List的成員函數(shù)sort進(jìn)行排序
方法2.用built-in函數(shù)sorted進(jìn)行排序(從2.4開始)

這兩種方法使用起來差不多,以第一種為例進(jìn)行講解:

從Python2.4開始,sort方法有了三個(gè)可選的參數(shù),Python Library Reference里是這樣描述的

復(fù)制代碼 代碼如下:

cmp:cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument:
"cmp=lambda x,y: cmp(x.lower(), y.lower())"
key:key specifies a function of one argument that is used to extract a comparison key from each list element: "key=str.lower"
reverse:reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.In general, the key and reverse conversion processes are much faster than specifying an
equivalent cmp function. This is because cmp is called multiple times for each list element while key and reverse touch each element only once.

以下是sort的具體實(shí)例。
實(shí)例1:
復(fù)制代碼 代碼如下:

>>>L = [2,3,1,4]
>>>L.sort()
>>>L
>>>[1,2,3,4]

實(shí)例2:
復(fù)制代碼 代碼如下:

>>>L = [2,3,1,4]
>>>L.sort(reverse=True)
>>>L
>>>[4,3,2,1]

實(shí)例3:
復(fù)制代碼 代碼如下:

>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>L.sort(cmp=lambda x,y:cmp(x[1],y[1]))
>>>L
>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

實(shí)例4:
復(fù)制代碼 代碼如下:

>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>L.sort(key=lambda x:x[1])
>>>L
>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

實(shí)例5:
復(fù)制代碼 代碼如下:

>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>import operator
>>>L.sort(key=operator.itemgetter(1))
>>>L
>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

實(shí)例6:(DSU方法:Decorate-Sort-Undercorate)
復(fù)制代碼 代碼如下:

>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>A = [(x[1],i,x) for i,x in enumerate(L)] #i can confirm the stable sort
>>>A.sort()
>>>L = [s[2] for s in A]
>>>L
>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

以上給出了6中對(duì)List排序的方法,其中實(shí)例3.4.5.6能起到對(duì)以List item中的某一項(xiàng)
為比較關(guān)鍵字進(jìn)行排序.
效率比較:
復(fù)制代碼 代碼如下:

cmp < DSU < key

通過實(shí)驗(yàn)比較,方法3比方法6要慢,方法6比方法4要慢,方法4和方法5基本相當(dāng)
多關(guān)鍵字比較排序:
實(shí)例7:
復(fù)制代碼 代碼如下:

>>>L = [('d',2),('a',4),('b',3),('c',2)]
>>> L.sort(key=lambda x:x[1])
>>> L
>>>[('d', 2), ('c', 2), ('b', 3), ('a', 4)]

我們看到,此時(shí)排序過的L是僅僅按照第二個(gè)關(guān)鍵字來排的,如果我們想用第二個(gè)關(guān)鍵字
排過序后再用第一個(gè)關(guān)鍵字進(jìn)行排序呢?有兩種方法
實(shí)例8:
復(fù)制代碼 代碼如下:

>>> L = [('d',2),('a',4),('b',3),('c',2)]
>>> L.sort(key=lambda x:(x[1],x[0]))
>>> L
>>>[('c', 2), ('d', 2), ('b', 3), ('a', 4)]

實(shí)例9:
復(fù)制代碼 代碼如下:

>>> L = [('d',2),('a',4),('b',3),('c',2)]
>>> L.sort(key=operator.itemgetter(1,0))
>>> L
>>>[('c', 2), ('d', 2), ('b', 3), ('a', 4)]

為什么實(shí)例8能夠工作呢?原因在于tuple是的比較從左到右之一比較的,比較完第一個(gè),如果
相等,比較第二個(gè)

相關(guān)文章

  • 樸素貝葉斯算法的python實(shí)現(xiàn)方法

    樸素貝葉斯算法的python實(shí)現(xiàn)方法

    這篇文章主要介紹了樸素貝葉斯算法的python實(shí)現(xiàn)方法,詳細(xì)分析了樸素貝葉斯算法的特性及用途,并給出了基于python的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2014-11-11
  • Python 進(jìn)程之間共享數(shù)據(jù)(全局變量)的方法

    Python 進(jìn)程之間共享數(shù)據(jù)(全局變量)的方法

    今天小編就為大家分享一篇Python 進(jìn)程之間共享數(shù)據(jù)(全局變量)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • numpy創(chuàng)建神經(jīng)網(wǎng)絡(luò)框架

    numpy創(chuàng)建神經(jīng)網(wǎng)絡(luò)框架

    本文介紹了使用numpy從零搭建了一個(gè)類似于pytorch的深度學(xué)習(xí)框架,可以用在很多地方,有需要的朋友可以自行參考一下
    2021-08-08
  • Python實(shí)現(xiàn)ElGamal加密算法的示例代碼

    Python實(shí)現(xiàn)ElGamal加密算法的示例代碼

    ElGamal加密算法是一個(gè)基于迪菲-赫爾曼密鑰交換的非對(duì)稱加密算法。這篇文章通過示例代碼給大家介紹Python實(shí)現(xiàn)ElGamal加密算法的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2020-06-06
  • django用戶注冊(cè)、登錄、注銷和用戶擴(kuò)展的示例

    django用戶注冊(cè)、登錄、注銷和用戶擴(kuò)展的示例

    本篇文章主要介紹了django用戶注冊(cè)、登錄、注銷和用戶擴(kuò)展的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • python?random模塊常用函數(shù)基礎(chǔ)教程

    python?random模塊常用函數(shù)基礎(chǔ)教程

    這篇文章主要為大家介紹了python?random模塊常用函數(shù)基礎(chǔ)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • 最新評(píng)論