python sort() sorted() argsort()函数
sort表示排序
sorted表示在原基础上创建副本排序
argsort函数返回的是数组值从小到大的索引值
默认是从小到大排序
sorted(iterable, cmp=None, key=None, reverse=True)
# -*- coding: utf-8 -*- from numpy import * import operator student_tuples = [('Jerry', 'C', 15), ('Tom', 'B', 12),('Bill', 'B', 10)] age_asc = sorted(student_tuples, key=operator.itemgetter(2)) print (age_asc) score_desc = sorted(age_asc, key=operator.itemgetter(1), reverse=True) print (score_desc)
结果如下:
[('Bill', 'B', 10), ('Tom', 'B', 12), ('Jerry', 'C', 15)] [('Jerry', 'C', 15), ('Bill', 'B', 10), ('Tom', 'B', 12)]
其中key=operator.itemgetter(1)表示从第几个元素排序