Python sorted sort python not only provides the List.sort () method to sort the list, but also provides the built-in sorted () function to sort the complex list and sort by the key and value of the dictionary. Sorted function prototypes
Sorted (data, Cmp=none, Key=none, reverse=False) #data is CMP and key are comparison function #Reverse is the sort direction, true is reverse, false is positive order
Basic usage for list, direct sort
>>> sorted ([5, 2, 3, 1, 4]) [1, 2, 3, 4, 5]>>> a = [5, 2, 3, 1, 4]>>&G T a.sort ()>>> a[1, 2, 3, 4, 5]
For dictionaries, only the key is sorted
' D ' ' B ' ' B ' ' E ' ' A ' }) [1, 2, 3, 4, 5]
Key function
The key function should accept a parameter and return a key value for sorting. Because the function only needs to be called once, the ordering is faster.
Complex list
>>> Student_tuples = [ ('John','A', 15), ('Jane','B', 12), ('Dave','B', 10),]>>> Sorted (Student_tuples, key=LambdaSTUDENT:STUDENT[2])#sort by age[('Dave','B', 10), ('Jane','B', 12), ('John','A', 15)]
If the list content is a class,
>>>classStudent:def __init__(self, name, grade, age): Self.name=name Self.grade=Grade Self.age= Agedef __repr__(self):returnrepr (Self.name, Self.grade, Self.age)>>> student_objects =[Student ('John','A', 15), Student ('Jane','B', 12), Student ('Dave','B', 10),]>>> Sorted (student_objects, key=LambdaStudent:student.age)#sort by age[('Dave','B', 10), ('Jane','B', 12), ('John','A', 15)]Dictionary
>>> student = [{"name":"xiaoming","score": 60}, {"name":"Daxiong","score": 20}, {"name":"Maodou","score": 30}, ]>>>student[{'score': 60,'name':'xiaoming'}, {'score': 20,'name':'Daxiong'}, {'score': 30,'name':'Maodou'}]>>> sorted (student, key=Lambdad:d["score"])[{'score': 20,'name':'Daxiong'}, {'score': 30,'name':'Maodou'}, {'score': 60,'name':'xiaoming'}]
In addition, Python provides operator.itemgetter and attrgetter to improve execution speed.
>>> fromoperatorImportItemgetter, Attrgetter>>> student = [ ("xiaoming", 60), ("Daxiong", 20), ("Maodou", 30}]>>> sorted (student, key=LambdaD:d[1])[('Daxiong', 20), ('Maodou', 30), ('xiaoming', 60)]>>> sorted (student, Key=itemgetter (1))[('Daxiong', 20), ('Maodou', 30), ('xiaoming', 60)]
Operator provides a complex sort of multiple fields.
# based on the first field and the second field [('daxiong'), ('maodou', "), (' Xiaoming', 60)]
The Operator.methodcaller () function calculates the sort according to the function provided.
>>> messages = ['Critical!!!','hurry!','Standby','immediate!!']>>> sorted (Messages, Key=methodcaller ('Count','!'))['Standby','hurry!','immediate!!','Critical!!!']
First pass the Count function to the "!" To calculate the number of occurrences, and then sort by the number of occurrences.
Cmp
The CMP parameter is the sort method used before Python2.4.
def numeric_compare (x, y): return x- y>>> sorted ([5, 2, 4, 1, 3], cmp=numeric_compare) [1, 2, 3, 4, 5]
def
reverse_numeric (x, y):
return y-
x>>> sorted ([5, 2, 4, 1, 3], CMP =
reverse_numeric) [5, 4, 3, 2, 1]
The Functools.cmp_to_key function provides a comparison function
>>> sorted ([5, 2, 4, 1, 3], key=Cmp_to_key (reverse_numeric)) [5, 4, 3, 2, 1]defCmp_to_key (mycmp):'Convert a cmp= function into a key= function' classK (object):def __init__(Self, obj, *args): Self.obj=objdef __lt__(self, Other):returnMYCMP (Self.obj, Other.obj) <0def __gt__(self, Other):returnMYCMP (Self.obj, other.obj) >0def __eq__(self, Other):returnMYCMP (self.obj, other.obj) = =0def __le__(self, Other):returnMYCMP (Self.obj, Other.obj) <=0def __ge__(self, Other):returnMYCMP (Self.obj, Other.obj) >=0def __ne__(self, Other):returnMYCMP (Self.obj, other.obj)! =0returnK
Python sorted sort