This article mainly introduces the python sorting method. It is very simple and practical to analyze Python's skills for default sorting, descending sorting, and sorting by key value, for more information about the python sorting method, see the following example. Share it with you for your reference. The details are as follows:
>>> Def my_key1 (x ):... return x % 10... >>> alist = [4, 5, 8, 1, 63, 8] >>> alist [4, 5, 8, 1, 63, 8] >>> alist. sort () # default Ascending Order >>> alist [1, 4, 5, 8, 8, 63] >>> alist. sort (reverse = True) # change to descending order> alist [63, 8, 8, 5, 4, 1]> alist. sort (key = my_key1) # Set the sort key value >>> alist [1, 63, 4, 5, 8, 8] >>>>>> def my_key2 (x) :... return x [1]... >>> alist = [(5, 'A'), (1, 'w'), (2, 'E'), (6, 'F')] >>> alist. sort (key = my_key2) # sort by the second component of each tuple> alist [(5, 'A'), (2, 'E'), (6, 'F'), (1, 'w')]>
I hope this article will help you with Python programming.