【问题标题】:Fastest way to sort tiny lists in python [closed]在python中对小列表进行排序的最快方法[关闭]
【发布时间】:2014-04-30 02:17:36
【问题描述】:

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

例如,要排序:

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

收件人:

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

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

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

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

谢谢。

【问题讨论】:

  • 是什么让你觉得内置函数开销太大?
  • Python 的内置排序实际上相当快。它根据输入的大小调整它使用的算法,对于小列表,它最终会使用插入排序。它也是用 C 语言实现的,所以它可能比你在纯 Python 中想出的任何东西都要快。见en.wikipedia.org/wiki/Timsort
  • 规则:永远不要使用冒泡排序。
  • 我的代码在排序时慢了 7 倍,这似乎很多,因为大约一半的列表已经排序。如果元素乱序,我只需要一种方法来交换元素,这看起来不像是指数运算

标签: python sorting


【解决方案1】:

是的。 如果列表的列表始终具有 2 个元素。使用 > 这样的操作符比使用 sorted 更快。

[(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

在您的情况下,您说您的列表有 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

时间:

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

因此,如果列表中有 2 或 3 个元素,则使用 sort_few 比使用 sorted 更快。

【讨论】:

    【解决方案2】:

    使用sorted() 非常有效:

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

    【讨论】:

    • 谢谢,但它不是子列表。这是一个对大小为 2...100000000 次的列表进行排序的函数,或者需要多少次。
    【解决方案3】:
    values = [{1, 5},{4, 2},{3, 7}]
    print(sorted(values))
    

    结果:

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

    【讨论】:

    • 他想对子列表进行排序,而集合是无序的,所以我根本不明白这如何回答他的问题。
    【解决方案4】:

    如果只有两个元素:

    f = lambda x: x if x[0] < x[1] else [x[1],x[0]]
    

    然后

    x = [5,4]
    f(x)
    

    这将返回 '[4,5]'

    x = [4,5]
    f(x)
    

    也返回 [4,5]

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 2016-08-30
      • 2014-10-06
      • 2012-04-12
      • 2012-05-05
      相关资源
      最近更新 更多