最近在看<<Python基础编程>>,里面第二章(列表和元组)里,有一段关于高级排序的讲解
原文:
如果希望元素能按照特定的方式进行排序(而不是sort函数默认的方式,即根据python的默认排序规则按升序排列元素),那么可以通过compare(x,y)形式自定义比较函数。
compare(x,y)函数会在x<y时返回负数,在x>y时返回正数,如果x=y则返回0(根据你的定义)。定义好该函数之后,就可以提供给sort方法作为参数了。内建函数cmp提供了比较函数的默认实现方式:
>>> cmp(42,32)
1
>>> cmp(99,100)
-1
>>> cmp(10,10)
0
>>> numbers = [5,2,9,7]
>>> numbers.sort(cmp)
>>> numbers
[2,5,7,9]
里面有讲到:定义好该函数后,就可以提供给sort方法作为参数了,但是如何定义?
还有,我看代码里用不用cmp作为sort的参数,numbers的输出都是一样的嘛,那,需要cmp参数干些什么呢在sort的方法里。
请大家帮忙回答一下,谢谢。
黄舟2017-04-17 12:03:25
sort
For the complete definition of parameters, see:
http://docs.python.org/2/library/functions.html#sorted
https://wiki.python.org/moin/HowTo /Sorting/
If I want to sort in descending order, then I need to customize the cmp method (of course the more convenient one is numbers.sort(reverse=True))
numbers=[5,2,9,7]
numbers.sort(lambda a,b:b-a)
numbers
[9,7,5,2]
If the array member is not a number, but another type such as dict, and you want to sort it based on a certain attribute
persons=[{'name':'zhang3','age':15},{'name':'li4','age':12}]
persons
[{'name':'zhang3','age':15},{'name':'li4','age':12}]
persons.sort(lambda a,b:a['age']-b['age'])
persons
[{'age': 12, 'name': 'li4'}, {'age': 15, 'name': 'zhang3'}]
ringa_lee2017-04-17 12:03:25
In fact, when the sort() method does not pass in the parameter func, the default cmp is None.
What is called is lambda x,y: cmp(x, y)
, but it actually calls the cmp function. That is:
numbers = [5,2,9,7]
numbers.sort()
#sort()函数判断cmp为None,则调用`lambda x,y: cmp(x, y)`
numbers.sort(cmp=None) #等效于numbers.sort()
numbers.sort(cmp=cmp)
#sort()判断cmp存在,并且不为None,则通过调用`cmp(x,y)`来排序
#虽然结果是相同的,可实际执行流程是不同的(这里我不确定sort()内部是用匿名函数还是直接用cmp来实现的,不过应该没有多大区别。)
numbers.sort(cmp)
#sort()通过*args来寻找是否存在没有指定变量的传入参数。
#函数判断其是否callalbe,如果是则赋值给cmp
#函数执行`cmp(x,y)`来排序
numbers.sort(custom_sort_method)
#sort()通过*args来寻找是否存在没有指定变量的传入参数。
#函数判断其是否callalbe,即是否是函数,如果是则赋值给cmp = custom_sort_method
# 函数调用`cmp(x,y)`来排序,但此时的cmp实际上已经是custom_sort_method方法了
numbers.sort(cmp = custom_sort_method)
# 此时函数直接发现cmp不为None,则直接执行cmp函数来排序,而此时cmp也已经是custom_sort_method方法了
If you want to implement a custom comparison function, you need to re-specify the comparison function constructed by cmp for you, as follows:
numbers = [5,2,9,7]
def reverse_numeric(x, y):
return y - x
numbers.sort(cmp = reverse_numeric)
#也可以直接写成
numbers.sort(revers_numeric)
#或者直接传入匿名函数
numbers.sort(lambda x,y: y-x)
In addition, the cmp parameter has been canceled in python3.x, and it is no longer supported to directly pass functions into sort(). You can construct a sorting function and pass it to the key.
ringa_lee2017-04-17 12:03:25
Python List sort() method application: http://www.w3cschool.cc/python/att-list-sort.html