python使用sorted函數對列表進行排序的方法

來源:互聯網
上載者:User
本文執行個體講述了python使用sorted函數對列表進行排序的方法。分享給大家供大家參考。具體如下:

python提供了sorted函數用於對列表進行排序,並且可以按照正序或者倒序進行排列

#建立一個數字組成的列表numbers = [5, 1, 4, 3, 2, 6, 7, 9] #輸出排序後的數字數組print sorted(numbers) #輸出原始數組,並未被改變print numbers my_string = ['aa', 'BB', 'zz', 'CC', 'dd', "EE"] #按字元順序對字串列表進行排序,預設為按字元順序排序print sorted(my_string) #使用reverse=True按照首字元的倒序進行排序print sorted(strs, reverse=True)## ['zz', 'aa', 'CC', 'BB']

希望本文所述對大家的Python程式設計有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.