Retrieve the two highest item from a list containing 100,000 integers

后端 未结 15 753
心在旅途
心在旅途 2020-12-13 17:55

How can retrieve the two highest item from a list containing 100,000 integers without having to sort the entire list first?

相关标签:
15条回答
  • 2020-12-13 18:11

    Without sorting the list the only way to really do it is to iterate through the whole list and save the highest two numbers. I think you'd be better off sorting the list.

    0 讨论(0)
  • 2020-12-13 18:12
    
    my_list = [20, 1, 9, 5, 10, 3, 4, 2, 11, 21, 2]
    
    max2 = 0
    max1 = 0
    for i in my_list:
        if i > max1:
            max1 = i
        elif max2 < i < max1:
            max2 = i
    
    print(f'max1: {max1}; max2: {max2}')
    max1: 21; max2: 11
    
    
    0 讨论(0)
  • 2020-12-13 18:16

    "2 highest" is impossible; only one item can be "highest". Perhaps you mean "highest 2". In any case, you need to say what to do when the list contains duplicates. What do you want from [8, 9, 10, 10]: (10, 9) or (10, 10)? If your response is (10, 10), please consider input of [8, 9, 10, 10, 10]. What are you going to do with the "highest two" when you've got them? Please edit your question to give this guidance.

    In the meantime, here's an answer that takes the first approach (two unique values):

    largest = max(inlist)
    second_largest = max(item for item in inlist if item < largest)
    

    You should add guards against fewer than 2 unique values in the list.

    0 讨论(0)
  • 2020-12-13 18:17

    Copy your List to List_copy. Retrieve the highest value and get its position by:

    Highest_value = max(List_copy)
    Highest_position = List_copy.index(max(List_copy))
    

    Assign 0 to the Highest_value.

    List_copy[Highest_position] = 0
    

    And run your line again.

    Second_Highest = max(List_copy)
    
    0 讨论(0)
  • 2020-12-13 18:17

    I know this topic is old, but here is a simple solution to this problem. Tested against heapq.nlargest and this is a bit faster (no sorting needed):

    Works for both positive and negative numbers.

    Function below: Max time used: 0.12, max memory used: 29290496 heapq.nlargest: Max time used: 0.14, max memory used: 31088640

    def two_highest_numbers(list_to_work):
    
        first = None
        second = None
    
        for number in list_to_work:
            if first is None:
                first = number
            elif number > first:
                second = first
                first = number
            else:
                if second is None:
                    second = number
                elif number > second:
                    second = number
    
    return [first, second]
    
    0 讨论(0)
  • 2020-12-13 18:18

    Use heapq.nlargest. This is the most flexible approach in case you ever want to handle more than just the top two elements.

    Here's an example.

    >>> import heapq
    >>> import random
    >>> x = range(100000)
    >>> random.shuffle(x)
    >>> heapq.nlargest(2, x)
    [99999, 99998]
    
    0 讨论(0)
提交回复
热议问题