In the python design language, there are many methods that can be used directly to operate elements of data types. Among them, the sorted method is to sort the elements of Python's basic data types, which can be numbers, strings, etc.
How to sort in python?
The steps are as follows:
The first step is to define a variable a and call the sorted method to sort the elements (strings) in the list, as shown in the figure below Display:
Related recommendations: "python video tutorial"
The second step, in addition to strings, the list can , it can also be of numerical type, as shown in the figure below:
#The third step, if you put positive numbers and negative numbers together and sort them according to their absolute values, you can Use the sorted method and add key=abs, as shown in the figure below:
The fourth step, if the sorted elements are strings, you can also use strings Sort by capital letters, as shown in the figure below:
The fifth step, in addition to sorting by capital letters of the string, you can also sort in reverse order, use the reverse attribute, as follows As shown in the picture:
The sixth step, if you use the lowercase letters of the string to sort, and do not use reverse order, as shown in the following picture:
The above is the detailed content of How to sort in python. For more information, please follow other related articles on the PHP Chinese website!