python中給列表元素排序


 

 1、永久排序

>>> test1 = ["ccc","ddd","aaa","bbb"] >>> test2 = test1[:] >>> test1 ['ccc', 'ddd', 'aaa', 'bbb'] >>> test2 ['ccc', 'ddd', 'aaa', 'bbb'] >>> test1.sort()  ## 正向排序 >>> test1 ['aaa', 'bbb', 'ccc', 'ddd'] >>> test2.sort(reverse=True)  ## 逆向排序 >>> test2 ['ddd', 'ccc', 'bbb', 'aaa'] >>> test3 = [3,5,1,8,2] >>> test5 = test3[:] >>> test3 [3, 5, 1, 8, 2] >>> test5 [3, 5, 1, 8, 2] >>> test3.sort() >>> test3 [1, 2, 3, 5, 8] >>> test5.sort(reverse=True) >>> test5 [8, 5, 3, 2, 1]

 

2、臨時排序

>>> test1 = [32,4,65,24,3,9] >>> test1 [32, 4, 65, 24, 3, 9] >>> sorted(test1) ## 臨時排序 [3, 4, 9, 24, 32, 65] >>> test1 [32, 4, 65, 24, 3, 9] >>> test2 = sorted(test1) >>> test1 [32, 4, 65, 24, 3, 9] >>> test2 [3, 4, 9, 24, 32, 65] >>> sorted(test1,reverse=True) [65, 32, 24, 9, 4, 3] >>> test3 = sorted(test1,reverse=True) >>> test1 [32, 4, 65, 24, 3, 9] >>> test3 [65, 32, 24, 9, 4, 3]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM