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

后端 未结 15 784
心在旅途
心在旅途 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: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)
    

提交回复
热议问题