[英]What arguments does Python sort() function have?
除了key
,是否还有其他参数,例如: value
?
sort
and sorted
sort
和sorted
参数 Both sort
and sorted
have three keyword arguments: cmp
, key
and reverse
. sort
和sorted
都有三个关键字参数: cmp
, key
和reverse
。
L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1
sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
Using key
and reverse
is preferred, because they work much faster than an equivalent cmp
. 首选使用
key
和reverse
,因为它们的工作速度比等效的cmp
快得多 。
key
should be a function which takes an item and returns a value to compare and sort by. key
应该是一个接受项目并返回值进行比较和排序的函数。 reverse
allows to reverse sort order. reverse
允许反向排序。
key
argument key
参数 You can use operator.itemgetter
as a key argument to sort by second, third etc. item in a tuple. 您可以使用
operator.itemgetter
作为关键参数来按元组中的第二,第三等进行排序。
>>> from operator import itemgetter
>>> a = range(5)
>>> b = a[::-1]
>>> c = map(lambda x: chr(((x+3)%5)+97), a)
>>> sequence = zip(a,b,c)
# sort by first item in a tuple
>>> sorted(sequence, key = itemgetter(0))
[(0, 4, 'd'), (1, 3, 'e'), (2, 2, 'a'), (3, 1, 'b'), (4, 0, 'c')]
# sort by second item in a tuple
>>> sorted(sequence, key = itemgetter(1))
[(4, 0, 'c'), (3, 1, 'b'), (2, 2, 'a'), (1, 3, 'e'), (0, 4, 'd')]
# sort by third item in a tuple
>>> sorted(sequence, key = itemgetter(2))
[(2, 2, 'a'), (3, 1, 'b'), (4, 0, 'c'), (0, 4, 'd'), (1, 3, 'e')]
Sequences can contain any objects, not even comparable, but if we can define a function which produces something we can compare for each of the items, we can pass this function in key
argument to sort
or sorted
. 序列可以包含任何对象,甚至不具有可比性,但是如果我们可以定义一个函数,该函数可以为每个项目进行比较,则可以将该函数传递给
key
参数进行sort
或sorted
。
itemgetter
, in particular, creates such a function that fetches the given item from its operand. itemgetter
尤其创建了一个从其操作数中获取给定项目的函数。 An example from its documentation: 文档中的示例:
After,
f=itemgetter(2)
, the callf(r)
returnsr[2]
.之后,
f=itemgetter(2)
,调用f(r)
返回r[2]
。
key
vs cmp
key
与cmp
Just out of curiosity, key
and cmp
performance compared, smaller is better: 出于好奇,
key
和cmp
性能的比较,较小的更好:
>>> from timeit import Timer
>>> Timer(stmt="sorted(xs,key=itemgetter(1))",setup="from operator import itemgetter;xs=range(100);xs=zip(xs,xs);").timeit(300000)
6.7079150676727295
>>> Timer(stmt="sorted(xs,key=lambda x:x[1])",setup="xs=range(100);xs=zip(xs,xs);").timeit(300000)
11.609490871429443
>>> Timer(stmt="sorted(xs,cmp=lambda a,b: cmp(a[1],b[1]))",setup="xs=range(100);xs=zip(xs,xs);").timeit(300000)
22.335839986801147
So, sorting with key
seems to be at least twice as fast as sorting with cmp
. 因此,使用
key
排序似乎至少是使用cmp
排序的两倍。 Using itemgetter
instead of lambda x: x[1]
makes sort even faster. 使用
itemgetter
代替lambda x: x[1]
使排序更快。
Besides key=
, the sort
method of lists in Python 2.x could alternatively take a cmp=
argument ( not a good idea, it's been removed in Python 3); 除了
key=
之外,Python 2.x中的列表sort
方法还可以采用cmp=
参数( 不是一个好主意,在Python 3中已将其删除); with either or none of these two, you can always pass reverse=True
to have the sort go downwards (instead of upwards as is the default, and which you can also request explicitly with reverse=False
if you're really keen to do that for some reason). 如果使用这两种方法中的一种或两种都不使用,则始终可以传递
reverse=True
使排序向下(而不是默认的向上排序),如果您真的很想这样做,也可以使用reverse=False
明确请求由于某些原因)。 I have no idea what that value
argument you're mentioning is supposed to do. 我不知道您提到的
value
论点应该做什么。
Yes, it takes other arguments, but no value
. 是的,它需要其他参数,但是没有
value
。
>>> print list.sort.__doc__
L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1
What would a value
argument even mean? value
争论甚至意味着什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.