#选择排序
def selectSort(L,key):
Len = len(L)
for i in range(Len-1,0,-1):
large = key(L[0])
index = 0
for j in range(1,i+1):
if key(L[j]) > large:
large = key(L[j])
index = j
L[index],L[i] = L[i],L[index]
if __name__ == '__main__':
L1 = [23,45,24,12,52,36,43,35,26,30]
L2 = ['afg','hszw','hna','nhed','asfw']
selectSort(L1,key=lambda item:item)
selectSort(L2,key=lambda item:item[2])
print L1,L2
结果如下
>>>
[12, 23, 24, 26, 30, 35, 36, 43, 45, 52] ['hna','nhed', 'asfw', 'afg', 'hszw']