在python中对小列表进行排序的最快方法

在python中对小列表进行排序的最快方法,python,sorting,Python,Sorting,我有大量非常小的列表需要尽快排序。通常这些列表中有2-3个值,而内置的排序方法似乎有太多的开销。在这种情况下,简单的气泡排序可以吗 例如,要排序: [1, 5] [4, 2] [3, 7] ... 致: 现在我正在做这样的事情: def do_something( ilist ): ilist = sorted(ilist, reverse = True); return ilist; for i in range(1000000000): do_something(

我有大量非常小的列表需要尽快排序。通常这些列表中有2-3个值,而内置的排序方法似乎有太多的开销。在这种情况下,简单的气泡排序可以吗

例如,要排序:

[1, 5]
[4, 2]
[3, 7]
...
致:

现在我正在做这样的事情:

def do_something( ilist ):
    ilist = sorted(ilist, reverse = True);
    return ilist;

for i in range(1000000000):
    do_something( [random_num,random_num ] );
谢谢。

使用非常有效:

>>> list_of_lists = [[1, 5], [4, 2], [3, 7]]
>>> sorted_lol = [sorted(sub_list) for sub_list in list_of_lists]
>>> sorted_lol
[[1, 5], [2, 4], [3, 7]]
结果:

[set([1, 5]), set([2, 4]), set([3, 7])]

如果只有两个元素:

f = lambda x: x if x[0] < x[1] else [x[1],x[0]]
这将返回“[4,5]”

x = [4,5]
f(x)
还返回[4,5]

如果超过两个,这是不起作用的,尽管你可以选择特殊情况3。这只是执行比较和交换,而不是调用完整的排序函数。

是。 如果列表列表始终包含2个元素。使用like
运算符比使用
排序的
更快

[(i[1], i[0]) if i[0]>i[1] else i for i in lst]
时间:

在你的例子中,你说你的列表有2到3个元素。排序函数如下所示

def sort_few(lst):
    if len(lst)==2:
        if lst[0] > lst[1]:
            return (lst[1], lst[0])
        else:
            return lst
    else:
        if lst[0] > lst[1]:
            if lst[1] > lst[2]:
                return (lst[2], lst[1], lst[0])
            else:
                return (lst[1], lst[2], lst[0])
        elif lst[1] > lst[2]:
            if lst[2] > lst[0]:
                return (lst[0], lst[2], lst[1])
            else:
                return (lst[2], lst[0], lst[1])
        elif lst[2] > lst[0]:
            if lst[0] > lst[1]:
                return (lst[1], lst[0], lst[2])
            else:
                return lst
时间:


因此,如果列表中有2或3个元素,那么使用
sort\u now
比使用
sorted
更快。

是什么让您认为内置函数的开销太大?Python的内置排序实际上相当快。它根据输入的大小调整它使用的算法,对于小列表,它只会使用插入排序。它也是用C实现的,所以它可能比纯Python中的任何东西都要快。请参阅规则:永远不要使用冒泡排序。排序时,我的代码可能会重复7倍,这似乎要慢很多,因为大约一半的列表已经排序。我只需要一个方法来交换无序的元素,这看起来不像指数运算。他想对子列表进行排序,而集合是无序的,所以我不知道这是如何回答他的问题的。谢谢,但不是子列表。这是一个对大小为2的列表进行排序的函数。。。100000000次或需要多少次。
x = [4,5]
f(x)
[(i[1], i[0]) if i[0]>i[1] else i for i in lst]
lst = [(0, 9),
       (1, 8),
       (2, 7),
       (3, 6),
       (4, 5),
       (5, 4),
       (6, 3),
       (7, 2),
       (8, 1),
       (9, 0)]

%timeit [(i[1], i[0]) if i[0]>i[1] else i for i in lst]
1000000 loops, best of 3: 1.96 us per loop

%timeit [sorted(i) for i in lst]
100000 loops, best of 3: 5.87 us per loop
def sort_few(lst):
    if len(lst)==2:
        if lst[0] > lst[1]:
            return (lst[1], lst[0])
        else:
            return lst
    else:
        if lst[0] > lst[1]:
            if lst[1] > lst[2]:
                return (lst[2], lst[1], lst[0])
            else:
                return (lst[1], lst[2], lst[0])
        elif lst[1] > lst[2]:
            if lst[2] > lst[0]:
                return (lst[0], lst[2], lst[1])
            else:
                return (lst[2], lst[0], lst[1])
        elif lst[2] > lst[0]:
            if lst[0] > lst[1]:
                return (lst[1], lst[0], lst[2])
            else:
                return lst
lst = [(1, 2, 3),
       (1, 3, 2),
       (2, 1, 3),
       (2, 3, 1),
       (3, 1, 2),
       (3, 2, 1),
       (1, 2, 3),
       (1, 3, 2),
       (2, 1, 3),
       (2, 3, 1),
       (3, 1, 2),
       (3, 2, 1)]


%timeit [sort_few(i) for i in lst]
100000 loops, best of 3: 6.3 us per loop

%timeit [sorted(i) for i in lst]
100000 loops, best of 3: 7.32 us per loop