Methods of sorting data in python

  • 2021-07-06 11:20:41
  • OfStack

The Python list has a built-in list. sort () method that allows you to modify the list in place. There is also a built-in function of sorted () to build a new sorted list from iteration. In this article, we will explore various techniques for sorting data using Python.

Note that the original data of sort () is corrupted, and sorted () does not operate on the original data, but creates a new data.

1. Basic sorting

The most basic sort is simple. Simply use the sorted () function to return a new sorted list


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

We can also use the list. sort () method. This method is an in-situ operation on the list list (the original data has been modified and is no longer what it was). In general, it is not as convenient as using sorted (), but if you don't need the original list list, it is more efficient to use sort ().


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

Another difference, list.sort() Method can only be applied to list object data. sorted (), on the other hand, can sort any iterable object. That is to say, sorted () is more commonly used. Here headlights are recommended for beginners to use sorted ().

2. Key parameter function

list. sort () and sorted () both have key parameters that specify functions to sort elements.

For example, here we use a string (string is also an iterable object)


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

We specify to sort by the first character (the character after the system 1 becomes lowercase). The value of the key parameter is a function with a single 1 parameter and returns a key key for sorting. This technology runs very fast. I don't understand why.

For example:


>>>student_tuples = [
... ('john', 'A', 15),
... ('jane', 'B', 12),
... ('dave', 'B', 10)]
>>>sorted(student_tuples, key = lambda student:student[2]) 
"""

Sort by key.

Value of key: lambda function

The arguments to the lambda function are elements-tuples in the student_tuples list

The lambda function operates on the third element of the tuple in the student_tuples list element.


"""
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

Similarly, this method can also act on the properties of objects. For example, here I first define 1 attribute


>>> 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) 
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

3. Method of Operator Library

The key function methods listed above are all very common, so python provides a concise and efficient method. operator library has three methods: itemgetter (), attrgetter () and methodcaller ().

Using the above-mentioned operator library method, the above example can also be implemented, and it is simpler and faster to run.


>>> 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 method of operator library can provide multi-dimensional sorting. For example, our team's grades and ages are ranked at the same time, but the priority of grades is higher than that of ages.


>>> 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

list. sort () and sorted () can be adjusted by reverse parameter (True or False). Here we flip the collation for the age age in the student data.


>>> 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)]

5. Sorting stability and complex sorting

Sort is stable, which means that when multiple records have the same key, the sort in the original data will be preserved.


>>> data = [('red', 1), ('blue', 1), ('red', 2), ('blue', 2)]
>>> sorted(data, key=itemgetter(0))
[('blue', 1), ('blue', 2), ('red', 1), ('red', 2)]

Note why blue in both records retains the data of the original data, so ('blue', 1) takes precedence over ('blue', 2) in the order of the original data.

This wonderful feature allows us to use a series of sorting steps to build complex sorting methods. For example, the score grade of student data is sorted in descending order, and then the age age is sorted in ascending order. Implementation method: firstly, sort age, and then sort grade.


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

6. Old Approach-Using cmp Parameters

cmp is also compare, compare and compare. In the 2. x era, cmp parameter method is supported.

In 3. x, the cmp parameter is completely removed.

In 2. x, sort allows an optional function to compare compare. This function should be taken to compare two parameters, and then return a negative value representing less than; Returns zero for equality; A positive value represents greater than. In 3. x, using the cmp idea, we can do this:


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

If you want to make the sort descending, you can use the following


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

Summarize


Related articles: