Both methods python list sorting and examples to explain

List of sorts, Python provides two methods

1. A method for sorting a built-in function list.sort List

list.sort(func=None, key=None, reverse=False)

Python example:

>>> list = [2,5,8,9,3]  
>>> list  
[2,5,8,9,3]  
>>> list.sort()  
>>> list  
[2, 3, 5, 8, 9]

2. A method for sorting a sequence type function sorted (list)

Python example:

在学习过程中有什么不懂得可以加我的
python学习交流扣扣qun,784758214
群里有不错的学习视频教程、开发工具与电子书籍。
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容
>>> list = [2,5,8,9,3]  
>>> list  
[2,5,8,9,3]  
>>> sorted(list)  
[2, 3, 5, 8, 9]

The difference between the two methods:

sorted (list) returns an object, can be used as an expression. The original list unchanged, generate a new sorted list object.

list.sort () does not return the object, change the original list.
Other sort of examples:

Example 1: Sort Forward

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

Example 2: Reverse ordering

>>>L = [2,3,1,4]
>>>L.sort(reverse=True)
>>>L
>>>[4,3,2,1]

Example 3: The second sort keywords

>>>L = [('b',6),('a',1),('c',3),('d',4)]
>>>L.sort(lambda x,y:cmp(x[1],y[1])) 
>>>L
>>>[('a', 1), ('c', 3), ('d', 4), ('b', 6)]

Example 4: Sort the second keyword

>>>L = [('b',6),('a',1),('c',3),('d',4)]
>>>L.sort(key=lambda x:x[1]) 
>>>L
>>>[('a', 1), ('c', 3), ('d', 4), ('b', 6)]

Example 5: The second sort keywords

>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>import operator
>>>L.sort(key=operator.itemgetter(1)) 
>>>L
>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

Example 6: (DSU Method: Decorate-Sort-Undercorate)

>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>A = [(x[1],i,x) for i,x in enumerate(L)] #i can confirm the stable sort
>>>A.sort()
>>>L = [s[2] for s in A]
>>>L
>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

6 given above method the List sort, examples can play 3.4.5.6 List Item in one pair in a

Sort the comparison key.

Efficiency comparison:

cmp < DSU < key

By experiments, the method of method 6 slower than 3, Method 4 The method is slower than 6, 4 and Method 5 The method substantially equivalent

Comparison of multi-keyword Sort:

Example 7:

在学习过程中有什么不懂得可以加我的
python学习交流扣扣qun,784758214
群里有不错的学习视频教程、开发工具与电子书籍。
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容
>>>L = [('d',2),('a',4),('b',3),('c',2)]
>>> L.sort(key=lambda x:x[1])
>>> L
>>>[('d', 2), ('c', 2), ('b', 3), ('a', 4)]

We see this time to sort out L is only in accordance with the second row of the keyword,

If we want to use the first row over the order after a sort keywords with the second keyword it? There are two ways

Example 8:

>>> L = [('d',2),('a',4),('b',3),('c',2)]
>>> L.sort(key=lambda x:(x[1],x[0]))
>>> L
>>>[('c', 2), ('d', 2), ('b', 3), ('a', 4)]

Example 9:

>>> L = [('d',2),('a',4),('b',3),('c',2)]
>>> L.sort(key=operator.itemgetter(1,0))
>>> L
>>>[('c', 2), ('d', 2), ('b', 3), ('a', 4)]

Example 8 Why can it work? The reason is that the tuple is compared from left to right comparison, a first comparison finished, if equal, comparing a second

Guess you like

Origin blog.51cto.com/14568144/2454894