• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            sunrise

            每天不斷學(xué)習(xí),才能不斷提升自己。

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              64 隨筆 :: 0 文章 :: 92 評論 :: 0 Trackbacks
            python列表排序 
            留著用的時候查查  轉(zhuǎn)自:http://gaopenghigh.iteye.com/blog/1483864
            簡單記一下python中List的sort方法(或者sorted內(nèi)建函數(shù))的用法。 

            關(guān)鍵字: 
            python列表排序 python字典排序 sorted 


            List的元素可以是各種東西,字符串,字典,自己定義的類等。 

            sorted函數(shù)用法如下: 
            Python代碼  收藏代碼
            1. sorted(data, cmp=None, key=None, reverse=False)  

            其中,data是待排序數(shù)據(jù),可以使List或者iterator, cmp和key都是函數(shù),這兩個函數(shù)作用與data的元素上產(chǎn)生一個結(jié)果,sorted方法根據(jù)這個結(jié)果來排序。 
            cmp(e1, e2) 是帶兩個參數(shù)的比較函數(shù), 返回值: 負(fù)數(shù): e1 < e2, 0: e1 == e2, 正數(shù): e1 > e2. 默認(rèn)為 None, 即用內(nèi)建的比較函數(shù). 
            key 是帶一個參數(shù)的函數(shù), 用來為每個元素提取比較值. 默認(rèn)為 None, 即直接比較每個元素. 
            通常, key 和 reverse 比 cmp 快很多, 因為對每個元素它們只處理一次; 而 cmp 會處理多次. 

            通過例子來說明sorted的用法: 

            1. 對由tuple組成的List排序 
            Python代碼  收藏代碼
            1. >>> students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10),]  


            用key函數(shù)排序(lambda的用法見 注釋1) 
            Python代碼  收藏代碼
            1. >>> sorted(students, key=lambda student : student[2])   # sort by age  
            2. [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]  


            用cmp函數(shù)排序 
            Python代碼  收藏代碼
            1. >>> sorted(students, cmp=lambda x,y : cmp(x[2], y[2])) # sort by age  
            2. [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]  


            用 operator 函數(shù)來加快速度, 上面排序等價于:(itemgetter的用法見 注釋2) 
            Python代碼  收藏代碼
            1. >>> from operator import itemgetter, attrgetter  
            2. >>> sorted(students, key=itemgetter(2))  


            用 operator 函數(shù)進行多級排序 
            Python代碼  收藏代碼
            1. >>> sorted(students, key=itemgetter(1,2))  # sort by grade then by age  
            2. [('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]  



            2. 對由字典排序 
            Python代碼  收藏代碼
            1. >>> d = {'data1':3, 'data2':1, 'data3':2, 'data4':4}  
            2. >>> sorted(d.iteritems(), key=itemgetter(1), reverse=True)  
            3. [('data4', 4), ('data1', 3), ('data3', 2), ('data2', 1)]  


            注釋1 
            參考:http://jasonwu.me/2011/10/29/introduce-to-python-lambda.html 

            注釋2 
            參考:http://ar.newsmth.net/thread-90745710c90cf1.html 
            class itemgetter(__builtin__.object) 
            |  itemgetter(item, ...) --> itemgetter object 

            |  Return a callable object that fetches the given item(s) from its operand. 
            |  After, f=itemgetter(2), the call f(r) returns r[2]. 
            |  After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3]) 

            相當(dāng)于 
            Python代碼  收藏代碼
            1. def itemgetter(i,*a):   
            2.     def func(obj):   
            3.         r = obj[i]   
            4.         if a:   
            5.             r = (r,) + tuple(obj[i] for i in a)   
            6.         return r   
            7.     return func   
            8.   
            9. >>> a = [1,2,3]   
            10. >>> b=operator.itemgetter(1)   
            11. >>> b(a)   
            12. 2   
            13. >>> b=operator.itemgetter(1,0)   
            14. >>> b(a)   
            15. (2, 1)   
            16. >>> b=itemgetter(1)   
            17. >>> b(a)   
            18. 2   
            19. >>> b=itemgetter(1,0)   
            20. >>> b(a)   
            21. (2, 1)   


            參考資料: 
            1. http://www.linuxso.com/linuxbiancheng/13340.html 
            2. http://www.douban.com/note/13460891/
            posted on 2012-04-27 11:28 SunRise_at 閱讀(5538) 評論(2)  編輯 收藏 引用 所屬分類: 可愛的python

            評論

            # re: python學(xué)習(xí)(二)--- python列表排序 2012-04-27 11:38 老魚
            提示:python 3.x已經(jīng)廢棄了cmp參數(shù)。  回復(fù)  更多評論
              

            # re: python學(xué)習(xí)(二)--- python列表排序 2012-04-27 11:43 SunRise_at
            這個我也不清楚,今兒就用到了第一個,我說留著用的時候再查呢。。。@老魚
              回復(fù)  更多評論
              

            欧美久久综合性欧美| 久久久99精品成人片中文字幕| 久久综合给合综合久久| 亚洲女久久久噜噜噜熟女| 国产美女久久精品香蕉69| 久久电影网一区| 91麻豆国产精品91久久久| 97久久精品无码一区二区| 亚洲国产成人精品女人久久久 | 狠狠久久亚洲欧美专区| 日本国产精品久久| 狠狠色丁香久久婷婷综合五月| 久久综合伊人77777麻豆| …久久精品99久久香蕉国产| 亚洲日本va午夜中文字幕久久| 九九99精品久久久久久| 麻豆av久久av盛宴av| 亚洲国产精品婷婷久久| 777午夜精品久久av蜜臀| 人人狠狠综合久久亚洲高清| 国产成人久久精品麻豆一区| 国产情侣久久久久aⅴ免费| 亚洲精品无码久久千人斩| 亚洲国产香蕉人人爽成AV片久久| 久久综合久久综合久久综合| 久久er99热精品一区二区| 囯产精品久久久久久久久蜜桃| 久久久精品国产亚洲成人满18免费网站| 人妻少妇久久中文字幕| 亚洲中文字幕无码久久2020 | 99久久精品国产一区二区蜜芽| 午夜精品久久久久久毛片| 精品久久久中文字幕人妻 | 99国产欧美精品久久久蜜芽| 久久久久久亚洲AV无码专区| 无码国内精品久久人妻蜜桃| 亚洲综合伊人久久综合| 国内精品久久久久影院日本| 91精品国产9l久久久久| 久久精品国产影库免费看| 天天爽天天爽天天片a久久网|