Fastest way to zero out low values in array?

后端 未结 9 1535
忘掉有多难
忘掉有多难 2020-12-24 07:07

So, lets say I have 100,000 float arrays with 100 elements each. I need the highest X number of values, BUT only if they are greater than Y. Any element not matching this

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-24 07:16

    Use a heap.

    This works in time O(n*lg(HighCountX)).

    import heapq
    
    heap = []
    array =  [.06, .25, 0, .15, .5, 0, 0, 0.04, 0, 0]
    highCountX = 3
    lowValY = .1
    
    for i in range(1,highCountX):
        heappush(heap, lowValY)
        heappop(heap)
    
    for i in range( 0, len(array) - 1)
        if array[i] > heap[0]:
            heappush(heap, array[i])
    
    min = heap[0]
    
    array = [x if x >= min else 0 for x in array]
    

    deletemin works in heap O(lg(k)) and insertion O(lg(k)) or O(1) depending on which heap type you use.

提交回复
热议问题