## Python3 list 排序函数详解

ztenv 2020-11-13 13:00:03
Python 函数 python3 排序 List

Python3 list 排序函数详解

list.sort(key=None,reverse=False)

(1)     key: 主要是用来进行比较的元素，只有一个参数，具体的函数的参数就是取自于可迭代对象中。指定可迭代对象中的一个元素来进行排序指定排序时使用的单个元素或多个元素、lambda表达式；

(2)     reverse：指字排序规则是升序还是降序，默认为升序排序；

1.      字符串排序

``````def list_sort_string():
list=["delphi","Delphi","python","Python","c++","C++","c","C","golang","Golang"]
list.sort() #按字典顺序升序排列
print("升序:",list)
list.sort(reverse=True) #按降序排列
print("降序:",list)``````

``````升序: ['C', 'C++', 'Delphi', 'Golang', 'Python', 'c', 'c++', 'delphi', 'golang', 'python']

2.      数值型排序

``````def list_sort_number():
list=[30,40,10,50,50.1,80,60,100,90]
list.sort()
print("升序:",list)
list.sort(reverse=True)
print("降序:",list)``````

``````升序: [10, 30, 40, 50, 50.1, 60, 80, 90, 100]

3.      根据列表中类对象的属性排序

``````class element(object):
def __init__(self,id="",name=""):
self.id=id
self.name=name
def __lt__(self, other): # override <操作符
if self.id<other.id:
return True
return False
def __str__(self): # override __str__
return "id={0},name={1}".format(self.id,self.name)
def sort_by_attribute():
list=[element(id="130",name="json"),
element(id="01",name="jack"),element(id="120",name="tom")]
list.sort()
for item in list:
print(item)``````

``````id=01,name=jack
id=120,name=tom
id=130,name=json``````

4.      根据列表中元素的长度排序

``````def list_sort_by_length():
list=["delphi","Delphi","python","Python","c++","C++","c","C","golang","Golang"]
list.sort(key=lambda ele:len(ele)) #按元素长度顺序升序排列
print("升序:",list)
list.sort(key=lambda ele:len(ele),reverse=True) #按降序排列
print("降序:",list)``````

``````升序: ['c', 'C', 'c++', 'C++', 'delphi', 'Delphi', 'python', 'Python', 'golang', 'Golang']

5.      根据列表中元素的多个属性进行排序：

``````def two_d_list_sort():
list=[ ["1","c++","demo"],
["1","c","test"],
["2","java",""],
["4","python","gil"],
["5","swift","apple"]
]
list.sort(key=lambda ele:ele[0])# 根据第1个元素排序
print(list)
list.sort(key=lambda ele:ele[1]) #先根据第2个元素排序
print(list)
list.sort(key=lambda ele:(ele[1],ele[0])) #先根据第2个元素排序，再根据第1个元素排序
print(list)``````

``````[['1', 'c++', 'demo'], ['1', 'c', 'test'], ['2', 'java', ''], ['4', 'python', 'gil'], ['5', 'swift', 'apple'], ['8', 'golang', 'google']]
[['1', 'c', 'test'], ['1', 'c++', 'demo'], ['8', 'golang', 'google'], ['2', 'java', ''], ['4', 'python', 'gil'], ['5', 'swift', 'apple']]
[['1', 'c++', 'demo'], ['1', 'c', 'test'], ['8', 'golang', 'google'], ['2', 'java', ''], ['4', 'python', 'gil'], ['5', 'swift', 'apple']]``````

6.      动态的根据用户指定的索引进行排序

``````排序索引: 0 [['1', 'c++', 'demo'], ['1', 'c', 'test'], ['2', 'java', ''], ['4', 'python', 'gil'], ['5', 'swift', 'apple'], ['8', 'golang', 'google']]

1、默认排序

2、根据类对象的单个属性进行排序，当然也可以扩展为根据类对象的多个属性进行排序

3、根据元素的固有属性进行排序，如：长度、第N个元素等。为了简单，所以借助了lambda表达式，当然也可以使用普通函数代替lambda表达式

4、动态的根据多个列进行排序，同时借助lambda和eval()函数实现

5、另外相比Python2，Python3取消了sort函数中的 cmp方式，只能用key方式。
所以python2用cmp方式写的函数迁移到python3中需要转换。
from functools import cmp_to_key

sort(iterable, key=cmp_to_key(cmp_fun)）