简体   繁体   English

Python sort()函数有哪些参数?

[英]What arguments does Python sort() function have?

除了key ,是否还有其他参数,例如: value

Arguments of sort and sorted sortsorted参数

Both sort and sorted have three keyword arguments: cmp , key and reverse . sortsorted都有三个关键字参数: cmpkeyreverse

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 . 首选使用keyreverse ,因为它们的工作速度比等效的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允许反向排序。

Using key argument 使用key参数

You can use operator.itemgetter as a key argument to sort by second, third etc. item in a tuple. 您可以使用operator.itemgetter作为关键参数来按元组中的第二,第三等进行排序。

Example

>>> 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')]

Explanation 说明

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参数进行sortsorted

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 call f(r) returns r[2] . 之后, f=itemgetter(2) ,调用f(r)返回r[2]

Mini-benchmark, key vs cmp 迷你基准测试, keycmp

Just out of curiosity, key and cmp performance compared, smaller is better: 出于好奇, keycmp性能的比较,较小的更好:

>>> 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM