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]

 

posted @ 2020-12-19 00:24  小鲨鱼2018  阅读(910)  评论(0编辑  收藏  举报