python sort() sorted() function

sort means sorting

sorted means to create a copy sort on the original basis

The default is to sort from small to large

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)

 

The result is as follows:

[('Bill', 'B', 10), ('Tom', 'B', 12), ('Jerry', 'C', 15)]
[('Jerry', 'C', 15), ('Bill', 'B', 10), ('Tom', 'B', 12)]
Where key=operator.itemgetter(1) means sorting from the first element

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324573257&siteId=291194637