Home >Backend Development >Python Tutorial >Which word is used for descending order in python?

Which word is used for descending order in python?

anonymity
anonymityOriginal
2019-06-11 11:43:504859browse

Python list's built-in sort() method is used for sorting. You can also use Python's built-in global sorted() method to sort iterable sequences to generate new sequences.

Which word is used for descending order in python?

1) Sorting Basics

Simple ascending sort is very easy. Just call the sorted() method. It returns a new list whose elements are sorted based on the less than operator (__lt__).

The code is as follows:

>>> sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]

You can also use the list.sort() method to sort, in which case the list itself will be modified. This method is generally less convenient than sorted(), but if you don't need to retain the original list, this method will be more efficient.

The code is as follows:

>>> a = [5, 2, 3, 1, 4]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5]

Another difference is that the list.sort() method is only defined in list, on the contrary, the sorted() method is valid for all iterable sequences.

The code is as follows:

>>> sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'})
[1, 2, 3, 4, 5]

2) key parameter/function

Starting from python2.4, list.sort() and sorted() functions The key parameter is added to specify a function that will be called before each element is compared. For example, ignore the case of a string through the function specified by key:

The code is as follows:

>>> sorted("This is a test string from Andrew".split(), key=str.lower)
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']

The value of the key parameter is a function. This function has only one parameter and returns a value for processing. Compare. This technique is fast because the function specified by key will be called exactly for each element.

A more widespread use case is to use certain values ​​​​of complex objects to sort the sequence of complex objects, for example:

The code is as follows:

>>> student_tuples = [
        ('john', 'A', 15),
        ('jane', 'B', 12),
        ('dave', 'B', 10),
]
>>> sorted(student_tuples, key=lambda student: student[2])   # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

The same technology has Complex objects with named attributes are also applicable. For example:

The code is as follows:

>>> class Student:
        def __init__(self, name, grade, age):
                self.name = name
                self.grade = grade
                self.age = age
        def __repr__(self):
                return repr((self.name, self.grade, self.age))
>>> student_objects = [
        Student('john', 'A', 15),
        Student('jane', 'B', 12),
        Student('dave', 'B', 10),
]
>>> sorted(student_objects, key=lambda student: student.age)   # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

3) Operator module function

The above key parameter is very useful Extensive, so python provides some convenience functions to make accessing methods easier and faster. The operator module has itemgetter, attrgetter, and methodcaller method has been added since 2.6. Using these methods, the above operation will become more concise and faster:

Copy the code The code is as follows:

>>> from operator import itemgetter, attrgetter
>>> sorted(student_tuples, key=itemgetter(2))
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
>>> sorted(student_objects, key=attrgetter('age'))
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

The operator module also allows multi-level sorting, for example, first by grade, and then Sort by age:

Copy code The code is as follows:

>>> sorted(student_tuples, key=itemgetter(1,2))
[('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]
>>> sorted(student_objects, key=attrgetter('grade', 'age'))
[('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]

4) Ascending and descending order

list.sort() and sorted() both Accepts a parameter reverse (True or False) to indicate ascending or descending sorting. For example, sort the students above in descending order as follows:

Copy code The code is as follows:

>>> sorted(student_tuples, key=itemgetter(2), reverse=True)
[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
>>> sorted(student_objects, key=attrgetter('age'), reverse=True)
[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]

The above is the detailed content of Which word is used for descending order in python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn