简体   繁体   English

Python内置的sorted()函数

[英]Built-in sorted() function in Python

I saw in a book how to pass a specific sorting function to Python's own built-in sorted() function as follows: 我在一本书中看到了如何将特定的排序函数传递给Python自己的内置sorted()函数,如下所示:

def mysort(a, b):
    if a[3] < b[3]:
        return -1
    elif a[3] > b[3]:
        return 1
    else:
        return 0

data = [
('Alpha Centauri A', 4.3, 0.26, 1.56),
('Alpha Centauri B', 4.3, 0.077, 0.45),
('Alpha Centauri C', 4.2, 0.00001, 0.00006),
("Barnard's Star", 6.0, 0.00004, 0.0005),
('Wolf 359', 7.7, 0.000001, 0.00002),
('BD +36 degrees 2147', 8.2, 0.0003, 0.006),
('Luyten 726-8 A', 8.4, 0.000003, 0.00006),
('Luyten 726-8 B', 8.4, 0.000002, 0.00004),
('Sirius A', 8.6, 1.00, 23.6),
('Sirius B', 8.6, 0.001, 0.003),
('Ross 154', 9.4, 0.00002, 0.0005),
]

sorted_data = sorted(data, mysort)

the code above sorts the data based on the 4th element of the 4-element tuple. 上面的代码根据4元素元组的第4个元素对数据进行排序。 Here, I am trying to figure out how the sorted() function feeds the a and b arguments to the mysort function. 在这里,我试图弄清sorted()函数如何将ab参数传递给mysort函数。 My intention is passing another argument to the mysort function similar to: 我的意图是将另一个参数传递给mysort函数,类似于:

def mysort(a, b, i):
    if a[i] < b[i]:
        return -1
    elif a[i] > b[i]:
        return 1
    else:
        return 0

where it will tell the function which element should the sorting be based on. 它将告诉函数排序应该基于哪个元素。 I am confused, because in the line 我很困惑,因为在排队

sorted_data = sorted(data, mysort)

we do not pass any arguments to the mysort function. 我们没有将任何参数传递给mysort函数。 The sorted() function seems to do its own magic, and provide the a and b arguments to the mysort function. sorted()函数似乎发挥了自己的魔力,并为mysort函数提供了ab参数。 To summarize, I wonder if there is a way to add a 3rd argument to the mysort function for different sorting types? 总而言之,我想知道是否可以为不同的排序类型在mysort函数中添加第3个参数?

Thank you! 谢谢!

You really want to use the key argument instead; 您确实想使用key参数; sorting on the 4th column with operator.itemgetter() : 使用operator.itemgetter()在第四列上排序:

from operator import itemgetter

sorted(data, key=itemgetter(3))

or you could use a lambda : 或者您可以使用lambda

sorted(data, key=lambda elem: elem[3])

or you could use functools.partial() : 或者您可以使用functools.partial()

from functools import partial

def mykeyfunc(column, item):
    return item[column]

sorted(data, key=partial(mykeyfunc, 3))

All 3 options create a new callable that is passed each item in data . 所有这三个选项创建一个新的可调用对象,该可调用对象将传递data每个项目。

The cmp argument to sorted() has been removed in Python 3. 在Python 3中已删除了sorted()cmp参数。

You usually don't sort with cmp (the second argument). 您通常不使用cmp排序(第二个参数)。 The key argument is the best choice 99% of the time: key论点是99%的时间是最佳选择:

def mysort(item):
    return item[3]

sorted_data = sorted(data, key=mysort)

Or more concisely: 或更简而言之:

sorted_data = sorted(data, key=lambda item: item[3])

To make your second function work, you need to create a function with your function: 为了使第二个功能正常工作,您需要使用以下功能创建一个功能:

def mysort(i):
    def sort_func(a, b)
        if a[i] < b[i]:
            return -1
        elif a[i] > b[i]:
            return 1
        else:
            return 0

    return sort_func

And use it like: 并像这样使用它:

sorted(data, mysort(3))

But a better way would be to use something builtin: 但是更好的方法是使用内置的东西:

from operator imoprt itemgetter

sorted_data = sorted(data, key=itemgetter(3))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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