python basic--sorted () functionSorting Algorithms
Sorting is also an algorithm that is often used in programs. Whether you use bubble sorting or fast sorting, the core of the sort is to compare the size of the two Elements. If it is a number, we can compare it directly, but what if it is a string or two dict? There is no point in directly comparing the size of mathematics, so the process of comparison must be abstracted by functions.
Python's sorted()
Built-in functions can sort the list:
>>> sorted ([5, -12, 9, -21]) [-21,-12, 5, 9, 36]
In addition sorted()
, the function is also a higher-order function, key
which can also receive a function to implement a custom sort, for example, by Absolute size:
>>> sorted ([5, -12, 9, -21], key=abs) [5, 9,-12,-21, 36]
the function specified by key acts on each element of the list and sorts according to the result returned by the key function . Compare the original list with key=abs
the processed list:
List = [5, -12, 9, -21= [, 5,, 9, 21]
The sorted()
function then sorts by keys and returns the corresponding elements of the list according to the corresponding relationship:
The keys sort result = [5, 9, a, a ,$ ]| | | | | Final result = = [5, 9,-12,-21, 36]
Let's look at another example of string ordering:
>>> Sorted (['Bob',' about','Zoo',' credit'])[' credit','Zoo',' about','Bob']
By default, the string is sorted by the size of ascii, because, as ‘Z‘ < ‘a‘
a result, uppercase letters are Z
preceded by lowercase letters. a
now, we propose that the sort should be ignored in case of alphabetical order. To implement this algorithm, you do not have to change the existing code much, as long as we can use a key function to map the string to ignore the case of Sorting. Ignoring the case to compare two strings is actually the first to capitalize the strings (or all Lowercase) before comparing Them.
In this way, sorted
We pass the key function and we can sort by ignoring the case:
>>> Sorted (['Bob',' about','Zoo',' credit'], key=Str.lower) [' about','Bob',' credit','Zoo']
To reverse sort, you can pass in the third parameter reverse=True
without having to change the key function:
>>> Sorted (['Bob',' about','Zoo',' credit'], key=str.lower, reverse=True) ['Zoo',' credit','Bob',' about']
As you can see from the above example, the abstraction of Higher-order functions is very powerful, and the core code can be kept very concise.
Summary
sorted()
is also a higher-order Function. The key to sorting is to implement a mapping Function. sorted()
Practice
Suppose we use a group of tuples to represent student names and scores:
L = [('Bob', '), ('Adam', "), ('Bart') , ('Lisa', 88)]
Please sort sorted()
the above list by name:
#practice:" "let 's say we use a group of tuples to represent student names and scores: L = [(' Bob ', ' +], (' Adam ', "), (' Bart ', 88)] 1, Please use sorted () to sort the above list by name: 2, Then sort by grades from highest to Lowest:" "L=[('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]#Sort by namedefby_name (t):returnT[0].lower ()Print('Sorted by Name:')Print(sorted (l,key=by_name))
Operation Result:
Then sort by grades from highest to Lowest:
# sort by high and low score def By_score (t): return t[1]print(")print(sorted ( L,key=by_score,reverse=true))
Operation Result:
Python basic--sorted () function