<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                **sorted函數** sorted 在Python3.5 中的源碼,只摘錄了下面咱們比較關注的部分 ~~~ def sorted(*args, **kwargs): # real signature unknown """ 默認返回一個包含所有排序元素的升序列表 Return a new list containing all items from the iterable in ascending order. 支持自定義鍵值方法的排序,并且可以設置逆序的標志將結果逆序。 A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order. """ pass ~~~ 排序在我們程序開發中是經常用到的功能。無論使用何種排序算法,排序的核心是比較兩個元素的大小。如果是數字,我們可以直接比較,但如果不是數字呢?直接比較數學上的大小就沒有意義了,因此,比較的過程必須通過函數抽象出來。 Python 內置的 sorted 方法對列表排序是非常強大的,我們通過下面幾個例子來簡單了解下。 實例 1:直接對數字列表排序 ~~~ >>> sorted([2,3,1,-1,0]) [-1, 0, 1, 2, 3] ~~~ 實例 2:自定義鍵值排序 ~~~ >>> sorted([2,3,1,-1,0],key=abs) [0, 1, -1, 2, 3] ~~~ 分析:sorted() 作為高階函數,還可以接收一個 key 函數來實現自定義的排序,例如按絕對值大小排序,求絕對值的函數會作用在列表的每個元素上形成新的列表 keys,新的列表作為排序的依據,進行排序 ~~~ 原數據 [ 2 , 3 , 1 , -1 , 0 ] keys [ 2 , 3 , 1 , 1 , 0 ] ~~~ 按照 keys 進行升序排 ~~~ keys排序 [ 0 , 1 , 1 , 2 , 3 ] 最終結果 [ 0 , 1 , -1 , 2 , 3 ] ~~~ 實例 3:自定義鍵值排序并逆序 ~~~ >>> sorted([2,3,1,-1,0],key=abs,reverse=True) [3, 2, 1, -1, 0] ~~~ 實例 4 : 對字符串排序 ~~~ >>> sorted(['airvip','lucy','Pytohon','php']) ['Pytohon', 'airvip', 'lucy', 'php'] ~~~ 分析:默認情況下,對字符串排序,是按照 ASCII 碼表的大小比較的,因為大寫字母在小寫字母前,所以大寫字母開頭的會排在小寫字母開頭的前面。 實例 5 :對列表中的元組按照元組的第二個值排序 ~~~ >>> L = [('b',1),('a',2),('d',3),('c',3)] >>> sorted(L,key=lambda x:x[1]) [('b', 1), ('a', 2), ('d', 3), ('c', 3)] ~~~ 我們看到,現在只是按照元組的第二個值排序,但我們還想按照元組中的第一個值排序,就需要 ~~~ >>> sorted(L,key=lambda x:(x[1],x[0])) [('b', 1), ('a', 2), ('c', 3), ('d', 3)] ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看