When we get a write data from a database, the general order of the list is a common problem, and today summarizes Python's general approach to listing list ordering:
First type: Built-in function sort ()
This should be our most used and simplest sort function, and you can sort the list directly.
Usage:
List.sort (Func=none, Key=none, Reverse=false (or True))
For reverse this type of bool parameter, when Reverse=false: is a positive sort; when reverse=true: sorts the direction. The default is, of course, false.
Will change the original list after execution
For example:
>>> list = [2,8,4,6,9,1,3]>>> list.sort () >>> list[1, 2, 3, 4, 6, 8, 9]
Second kind: Sequence type function sorted ()
The difference between this and the first is that it does not change the original list, but instead returns an object
Usage:
Sorted (list)
As well as the first method, the function also contains reverse of this type bool, when Reverse=false: in a positive order (small to large), when reverse=true: in reverse order (from largest to smallest). The default is, of course, false.
After execution, there will be a new sorted list object returned
For example:
>>> list = [2,8,4,1,5,7,3]>>> other = sorted (list) >>> other[1, 2, 3, 4, 5, 7, 8]
Of course, sometimes we encounter this situation, that is, each element in the list is more than one element (such as the list, the element is the Ganso type), we want to sort the first keyword, but also on the basis of the first one based on the second keyword to sort , Then we can use the third method.
The third type:
This approach is actually based on the second kind of sorted () function sort extension. A lambda expression is required here.
Let's look at an example:
We want to sort the first keyword of the element in the list and then sort by the second keyword on the basis of the first element to see the result:
>>> list = [(' d ', 3), (' A ', 5), (' d ', 1), (' C ', 2), (' d ', 2)]>>> print sorted (list, key = lambda x: (x[0],x[1]) ) [(' A ', 5), (' C ', 2), (' d ', 1), (' d ', 2), (' d ', 3)]
There is also a:
Today, we encounter a problem with a string, such as F10 in the back of F2 . A method has been found for reference:
Reference Address: http://blog.csdn.net/houyj1986/article/details/22966799
#encoding =utf-8 print ' China ' #根据字符串中的数字排序, such as F10 should be in F2 after import re re_digits = Re.compile (R ' (\d+) ') def emb_numbers (s): Pieces=re_digits.split (s) Pieces[1::2]=map (Int,pieces[1::2]) return pieces def sort_strings_with_emb_numbers (alist): aux = [(Emb_numbers (s), s) for S in Alist] aux.sort () return [s for __,s in aux] def sort_strings_with_emb_numbers2 (alist): return sorted (alist, key=emb_numbers) C14/>filelist= ' file10.txt file2.txt file1.txt '. Split () print filelist print '--dsu sort ' print Sort_ Strings_with_emb_numbers (filelist) print '--built-in DSU sort ' print sort_strings_with_emb_numbers2 (filelist)
The printing results are as follows:
China [' file10.txt ', ' file2.txt ', ' file1.txt ']--dsu sort [' file1.txt ', ' file2.txt ', ' file10.txt ']--built-in DSU sort [' file1.txt ', ' File2.txt ', ' file10.txt ']
Sort the list of Python lists