python sorted函数是对可迭代对象的排序,参数有cmp,key,reverse.
cmp:比较的函数,有两个参数,大于返回1,小于返回-1,等于返回0。
key:用来比较进行排序的元素。
reverse:排序的规则,reverse = True是降序。
a = [1,2,4,6,8,3,5,7]
b = ['a','b','c','e','o','t','f','j']
c = zip(a,b)
sorted(c,key=lambda x:x[0])
[(1, 'a'),(2, 'b'),(3, 't'),(4, 'c'), (5, 'f'),(6, 'e'),(7, 'j'),(8, 'o')]
a = [1,2,4,6,8,3,5,7]
b = ['a','b','c','e','o','t','f','j']
c = zip(a,b)
sorted(c,key=lambda x:x[1],reverse = True)
[(3, 't'),(8, 'o'),(7, 'j'),(5, 'f'),(6, 'e'),(4, 'c'),(2, 'b'),(1, 'a')]
参考链接:Python sorted() 函数