how to efficiently get the k bigger elements of a list in python

后端 未结 5 613
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 06:58

What´s the most efficient, elegant and pythonic way of solving this problem?

Given a list (or set or whatever) of n elements, we want to get the k biggest ones. ( Yo

相关标签:
5条回答
  • 2020-12-05 07:13

    The simple, O(n log n) way is to sort the list then get the last k elements.

    The proper way is to use a selection algorithm, which runs in O(n + k log k) time.

    Also, heapq.nlargest takes O(k log n) time on average, which may or may not be good enough.

    (If k = O(n), then all 3 algorithms have the same complexity (i.e. don't bother). If k = O(log n), then the selection algorithm as described in Wikipedia is O(n) and heapq.nlargest is O(n log log n), but double logarithm is "constant enough" for most practical n that it doesn't matter.)

    0 讨论(0)
  • 2020-12-05 07:19
    sorted(l, reverse=True)[:k]
    
    0 讨论(0)
  • 2020-12-05 07:22

    Use nlargest from heapq module

    from heapq import nlargest
    lst = [9,1,6,4,2,8,3,7,5]
    nlargest(3, lst) # Gives [9,8,7]
    

    You can also give a key to nlargest in case you wanna change your criteria:

    from heapq import nlargest
    tags = [ ("python", 30), ("ruby", 25), ("c++", 50), ("lisp", 20) ]
    nlargest(2, tags, key=lambda e:e[1]) # Gives [ ("c++", 50), ("python", 30) ]
    
    0 讨论(0)
  • 2020-12-05 07:22
    l = [9,1,6,4,2,8,3,7,5]
    
    sorted(l)[-k:]
    
    0 讨论(0)
  • 2020-12-05 07:30

    You can use the heapq module.

    >>> from heapq import heapify, nlargest
    >>> l = [9,1,6,4,2,8,3,7,5]
    >>> heapify(l)
    >>> nlargest(3, l)
    [9, 8, 7]
    >>> 
    
    0 讨论(0)
提交回复
热议问题