Python list sorting and Python list sorting
Python list sorting
This article is reprinted, Source Address: http://blog.csdn.net/horin153/article/details/7076321
In Python, to sort a list, you can use list. sort () or sorted (iterable [, cmp [, key [, reverse]).
Where:
Cmp (e1, e2) is a comparison function with two parameters. Return Value: negative: e1 <e2, 0: e1 = e2, positive: e1> e2. The default value is None, use the built-in comparison function.
Key is a function with a parameter used to extract a comparison value for each element. The default value is None, that is, to directly compare each element.
Reverse is a Boolean value indicating whether to reverse the comparison result.
I used to write a custom cmp function when doing complex sorting. After reading the Python documentation, I found that my practice is not good. A simple description is as follows:
1. Generally, key and reverse are much faster than cmp, because each element is processed only once, and cmp will process multiple times.
In other words, writing key functions is much more efficient than writing cmp functions.
2. You do not need to write cmp functions for some seemingly complex sorting functions. For example:
>>> Students = [('john', 'A', 15), ('Jane ',' B ', 12), ('Dave', 'B ', 10),]
Key using element indexes:
>>> Sorted (students, key = lambda student: student [2]) # sort by age
[('Dave ',' B ', 10), ('Jane', 'B', 12), ('john', 'A', 15)]
Use the attribute named by the element as the key:
>>> Sorted (students, key = lambda student: student. age) # sort by age
[('Dave ',' B ', 10), ('Jane', 'B', 12), ('john', 'A', 15)]
The operator function is used to accelerate the speed. The above two sorting operations are equivalent:
>>> From operator import itemgetter, attrgetter
>>> Sorted (students, key = itemgetter (2 ))
>>> Sorted (students, key = attrgetter ('age '))
Using the operator function for multi-level sorting is a complicated application. According to my previous understanding, we must write a custom cmp function. Now it seems that it was really stupid.
# Sort by grade then by age
>>> Sorted (students, key = itemgetter (1, 2 ))
[('John', 'A', 15), ('Dave ',' B ', 10), ('Jane', 'B', 12)]
>>> Sorted (students, key = attrgetter ('grad', 'age '))
[('John', 'A', 15), ('Dave ',' B ', 10), ('Jane', 'B', 12)]
3. sort by dictionary Value
>>> D = {'A': 2, 'B': 23, 'C': 5, 'D': 17, 'E': 1}
#1. Return the pair:
From operator import itemgetter
>>> Sorted (d. iteritems (), key = itemgetter (1), reverse = True)
[('B', 23), ('D', 17), ('C', 5), ('A', 2), ('E ', 1)]
#2. Only keys are returned:
>>> Sorted (d, key = d. _ getitem __, reverse = True)
['B', 'D', 'C', 'A', 'E']
4. sorted () returns a new sorted list.
List. sort () is an in-place sorting to save space. Of course, a new list in the sorting order will not be returned. This sorting of the large list has a space advantage.
Python list sorting
[(I, j) for j, I in sorted ([(a, B) for B, a in l], reverse = True)]
You can also
L. sort (key = lambda x: x [1], reverse = True)
How to sort the python list, such as [1, 3, 5, 2, 8], to write a program, you cannot use sort
#-*-Coding: UTF-8 -*-
A = [1, 3, 5, 2, 8]
For I in range (len ()):
For j in range (I + 1, len ()):
If a [I]> a [j]:
A [I], a [j] = a [j], a [I] # This sentence is handsome.
Print